How I can "merge" an nested array?

image

I have an array where each element has an array of elements. how can have a unique flat array?

I mean one unique array with all children elements. I have been fighting with this since yesterday

1 Like

Can you provide an example response so we can see what we are actually working with.

What is that a snippet from? It doesn’t look like the console log.

JSON is key value pairs, but the value can be a string, an object, or another array.

I can’t tell exactly what you have there (where it says 10 keys).

It doesn’t look like an array or object (although I suspect they are objects).

If I can see what the response actually looks like, I can probably advise on how to flatten it if its possible.

Please use the preformatted text option in the editor if you paste code or JSON (so everything isn’t aligned to the left). Also so we can copy and paste to mock the response.

Example

[
  [
    {"id": 1, "name": "lulu"},
    {"id": 2, "name": "car"},
    {"id": 3, "name": "tree"}
  ],
  [
    {"id": 4, "name": "computer"},
    {"id": 5, "name": "screen"}
  ],
  [
    {"id": 6, "name": "vs code"}
  ]
]

The expected result will be:

[
  {"id": 1, "name": "lulu"},
  {"id": 2, "name": "car"},
  {"id": 3, "name": "tree"},
  {"id": 4, "name": "computer"},
  {"id": 5, "name": "screen"},
  {"id": 6, "name": "vs code"}
]
1 Like

The question is for post-flows. because I don’t see a concat() function in FQL in the append but when I tried to use it is not working.

Sorry, just noticed the Postman flows tag.

I don’t use Postman flows, so hopefully someone with experience of that functionality will be along to shed some light.

Thank you for your post .It is engaging. I may alter slightly your example:

[
  [
    {"id": 1, "name": "lulu"},
    {"id": 2, "name": "car"},
    {"id": 3, "name": "tree"}
  ],
  [
    {"id": 4, "name": "computer"},
    {"id": 5, "name": "screen"}
  ],
  [
    {"id": 6, "name": "vs code"}
  ]
]

like this.

{
    "0": [
        { "id": "0",  "name": "lunar" },
        { "id": "1",  "name": "drive" },
        {"id": "2","name": "ride" }
    ],
    "1": [
        {  "id": "3", "name": "lunar" },
        { "id": "4", "name": "drive"  }
    ],
    "2": [
        { "id": "5", "name": "lunar"  }
    ]
}

@supply-candidate-367 If you have an export of your collection and your environment , I may help

Hi @supply-candidate-367

You can use the $map method to iterate over each sub-array and the $merge method to flatten the array into a single object with all keys present.

Example: