# Processing multiple nested arrays

**URL:** <https://discourse.nodered.org/t/processing-multiple-nested-arrays/45919>\
**Category:** General\
**Created:** [19 May 2021 12:58 UTC](https://discourse.nodered.org/t/processing-multiple-nested-arrays/45919 "2021-05-19T12:58:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sidr](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@Sidr](https://discourse.nodered.org/u/Sidr)\
**Post date:** [19 May 2021 12:58 UTC](https://discourse.nodered.org/t/processing-multiple-nested-arrays/45919/1 "2021-05-19T12:58:39Z")

</div>

Hello everyone,

I have an input with nested arrays with the structure as shown below:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2f9c1a40869f8dcb0c5a6a0c5facc6a16b5f487a.png)

The main array has 6 elements. Each of the main 6 elements has 251 sub-arrays. And each of the 251 sub-array has 4 elements.

I would like to transform the array in such a way that the output array has 251 elements with each element having the the individual element from the main array. For example, if we consider  
Input:  
[[1,2,3,4....251],[1,2,3,4....251],.........[1,2,3,4....251],[1,2,3,4....251]],

I would like to to be transformed as  
output:  
[[1,1,1,1...1],[2,2,2,....2].......[251,251,251,....251]

How can this be done?

Thank you for your time

Regards,

Sid

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [19 May 2021 13:03 UTC](https://discourse.nodered.org/t/processing-multiple-nested-arrays/45919/2 "2021-05-19T13:03:52Z")

</div>

If you supply a small example of the array you wish to transform maybe someone will take a crack at it. But I doubt anyone would want to create the sample as that would be your job.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [19 May 2021 17:01 UTC](https://discourse.nodered.org/t/processing-multiple-nested-arrays/45919/3 "2021-05-19T17:01:02Z")

</div>

May be this way. Can't say anything about performance

```auto
/*
msg.payload = [
    ['a','b','c','d'],
    ['aa','bb','cc','dd'],
    ['aaa','bbb','ccc','ddd'],
    ['aaaa','bbbb','cccc','dddd']
]
*/

let result = Array.from(Array(msg.payload.length), () => []);
msg.payload.forEach((arr) => {
    arr.forEach((a,i) => {
        result[i].push(a)
    })
})

msg.payload = result
return msg;

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 May 2021 17:39 UTC](https://discourse.nodered.org/t/processing-multiple-nested-arrays/45919/4 "2021-05-19T17:39:12Z")

</div>

> [@hotNipi](#):
>
> `let result = Array.from(Array(msg.payload.length), () => []);`

Only needs to be:

```auto
const result = []

```

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [19 May 2021 17:49 UTC](https://discourse.nodered.org/t/processing-multiple-nested-arrays/45919/5 "2021-05-19T17:49:23Z")

</div>

I understood that the task is to swap rows with columns in multidimensional array. So the result has same structure with empty arrays in wrapper array prepared before iteration.  
Internal arrays can of course be added at first level of iteration but is there any difference I don't know.

Edit:  
Seems that I have misread the task so it is far from solution ...

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [19 May 2021 18:08 UTC](https://discourse.nodered.org/t/processing-multiple-nested-arrays/45919/6 "2021-05-19T18:08:03Z")

</div>

This way then

```auto
let result = []
msg.payload.forEach((arr) => {
    arr.forEach((a,i) => {
        if(!result[i]){
            result.push([])
        }
        result[i].push(a)
    })
})

msg.payload = result
return msg;

```

---

<div class="post-metadata">

**Author:** ![Sidr](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@Sidr](https://discourse.nodered.org/u/Sidr)\
**Post date:** [20 May 2021 07:29 UTC](https://discourse.nodered.org/t/processing-multiple-nested-arrays/45919/7 "2021-05-20T07:29:33Z")

</div>

Yes.. that works.. Thank you very much 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 July 2021 07:30 UTC](https://discourse.nodered.org/t/processing-multiple-nested-arrays/45919/8 "2021-07-19T07:30:17Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
