Pushing objects in to array

#1

Hello,

Quite new to node-red but have a bit of a confusing thing that i would like to have clarification from more experienced node-red developers.

I have a function which i would like to use to return an array of objects:

var outputMsg = [];

testiObject = {
“name”:"",
“value”:""
};

var testName = [“works”,“yes”,“no”];
var testValue = [1,2,3];

test1.name = testName[0];
test1.value = testValue[0];

test2.name = testiName[1];
test2.value = testiValue[1];

What i would like to do now is to push test1 and test2 to to outputMsg and then return the array of 2 objects as a result:

outputMsg.push(test1);
outputMsg.push(test2);

return [ outputMsg ];

Now for some reason when i pass outputMsg to debug node the result is:

{“name”:“yes”,“value”:2,"_msgid":“b993db05.6c75c8”}
{“name”:“yes”,“value”:2,"_msgid":“b993db05.6c75c8”}

As it should naturally be:

{“name”:“works”,“value”:1,"_msgid":“b993db05.6c75c8”}
{“name”:“yes”,“value”:2,"_msgid":“b993db05.6c75c8”}

Can someone please explain to me why this happens and what am doing wrong here?

I know that i could do this correctly like this:

outputMsg.push({payload:{name:test1.name, value:test1.value}});
outputMsg.push({payload:{name:test2.name, value:test2.value}});
return [ outputMsg ];

But i would like to know what goes wrong with trying to push objects test1 and test2 to outputMsg array?

Thank you if you can find time to answer this thing that i just can’t get my head around to. :slight_smile:

#2

should there be the ‘i’ in those two lines??

#3

Naturally if i was using a for loop, which i left out to simplify the example code.

The original post is missing lines:

test1 = testObject;
test2 = testObject;

before var testName line.

#4

could you post the flow so people can see exactly what you have? (or at least the function node)

#5

So that means test1 and test2 both point at the same object. If you update one, you update them both.

You can use RED.util.cloneMessage to create a copy you can update independently:

var test1 = RED.util.cloneMessage(testObject);
var test2 = RED.util.cloneMessage(testObject);
#6

If the ultimate goal is to generate such array of objects then the final code in your function node could be as simple as below (though some error checking would be advisable):

var outputMsg = [];

var testName = ["works","yes","no"];
var testValue = [1,2,3];

while (testName.length > 0) {
name =  testName.shift(); 
value = testValue.shift();
outputMsg.push({name, value});
}

return [ outputMsg ];
#7

Thx, this explains the why. Would using new testObject have changed anything or is RED.util.cloneMessage the only solution for this in nodered?

var test1 = new testObject;
var test2 = new testObject;

And I have realized that I probably should check a few more tutorials regarding nodered as their seems to be some concepts which I need understand and digest which differ from “normal” coding thinking patterns. :slight_smile:

#8

Because that creates new objects, not just a pointer to the existing one.