Getting an array from flow context

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.

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?

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!!

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

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

example to see if this is what you wanted

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

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
}

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

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