# Getting an array from flow context

**URL:** <https://discourse.nodered.org/t/getting-an-array-from-flow-context/74942>\
**Category:** General\
**Created:** [9 February 2023 14:04 UTC](https://discourse.nodered.org/t/getting-an-array-from-flow-context/74942 "2023-02-09T14:04:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [9 February 2023 14:04 UTC](https://discourse.nodered.org/t/getting-an-array-from-flow-context/74942/1 "2023-02-09T14:04:41Z")

</div>

Im sure Im missing something simple here.

Im pushing some items onto an array then saving to the flow context.

I do this in a few different function blocks.

Another function block will get all of these arrays from the flow context for further processing. This is where Im stuck.

I can get the array, and send it straight into a debug and it is an object, containing an array of x items. This all looks good.

The job is to simply grab the context, and push everything onto a single array.

Here is that last function, and the output I was getting was empty, I know this code doesnt make sense but thats because I tried a whole bunch of things before giving up for the day.

```auto
let newObj = [];

let av = flow.get('av');
let io = flow.get('io');

// av.forEach(function(element) {
// newObj.push(element);
// });

// io.forEach(function(element) {
// newObj.push(element);
// });

for (let index = 0; index < av.length; index++) {
    // newObj.push av[index];
    node.send(av);
}

// msg.payload = av;

return msg;

```

Specifically, why couldnt I push the value onto the new object here?

```auto
for (let index = 0; index < av.length; index++) {
    // newObj.push av[index];
    node.send(av);
}

```

and Im sure theres a better way to do what Im doing here which Id be happy to go down that road once I get the basics sorted. Ill probably rewrite all of this once I know whats happening as need the data ordered correctly so I think I should use objects or do better error checking with the array as the order is going to be important. But thats a seperate thing for now. Thanks!!

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [9 February 2023 14:45 UTC](https://discourse.nodered.org/t/getting-an-array-from-flow-context/74942/2 "2023-02-09T14:45:40Z")

</div>

> [@steven\_191](#):
>
> `newObj.push av[index];`

I think you need:  
`newObj.push(av[index])`

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [10 February 2023 17:58 UTC](https://discourse.nodered.org/t/getting-an-array-from-flow-context/74942/3 "2023-02-10T17:58:55Z")

</div>

To get `av` & `io` into `newObj` outside of the loop try `newObj = [...av, ...io]`

example to see if this is what you wanted

```auto
let z = []
const x = [1, 2, 3, 4, 5]
const y = [6, 7, 8, 9, 10]

z = [...x, ...y]

/*
z = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*/

```

Did you mean

```auto
for (let index = 0; index < av.length; index++) {
    // newObj.push av[index];
    node.send(av[index]); // Otherwise you are sending the Array av however long is times
}

```

---

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [13 February 2023 09:01 UTC](https://discourse.nodered.org/t/getting-an-array-from-flow-context/74942/4 "2023-02-13T09:01:23Z")

</div>

Im sure I tried that method suggested which is why I was assuming Im doing something wrong but theres a chance I missed it.

And Ive rewritten what I was trying to do and did find the spread function which should fulfill my requirements as I was wrong about the data needing to be ordered, a JS object will be suitable so that makes things easier.

Thanks

---

<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:** [14 April 2023 09:01 UTC](https://discourse.nodered.org/t/getting-an-array-from-flow-context/74942/5 "2023-04-14T09:01:24Z")

</div>

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