# Pushing objects in to array

**URL:** <https://discourse.nodered.org/t/pushing-objects-in-to-array/587>\
**Category:** General\
**Created:** [3 June 2018 22:50 UTC](https://discourse.nodered.org/t/pushing-objects-in-to-array/587 "2018-06-03T22:50:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![toni](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@toni](https://discourse.nodered.org/u/toni)\
**Post date:** [3 June 2018 22:50 UTC](https://discourse.nodered.org/t/pushing-objects-in-to-array/587/1 "2018-06-03T22:50:00Z")

</div>

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. 🙂

---

<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:** [3 June 2018 22:59 UTC](https://discourse.nodered.org/t/pushing-objects-in-to-array/587/2 "2018-06-03T22:59:26Z")

</div>

> [@toni](#):
>
> test2.name = testiName[1];  
> test2.value = testiValue[1];

should there be the 'i' in those two lines??

---

<div class="post-metadata">

**Author:** ![toni](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@toni](https://discourse.nodered.org/u/toni)\
**Post date:** [3 June 2018 23:03 UTC](https://discourse.nodered.org/t/pushing-objects-in-to-array/587/3 "2018-06-03T23:03:31Z")

</div>

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.

---

<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:** [3 June 2018 23:09 UTC](https://discourse.nodered.org/t/pushing-objects-in-to-array/587/4 "2018-06-03T23:09:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [3 June 2018 23:12 UTC](https://discourse.nodered.org/t/pushing-objects-in-to-array/587/5 "2018-06-03T23:12:54Z")

</div>

> [@toni](#):
>
> test1 = testObject;  
> test2 = testObject;

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:

```auto
var test1 = RED.util.cloneMessage(testObject);
var test2 = RED.util.cloneMessage(testObject);

```

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [4 June 2018 01:27 UTC](https://discourse.nodered.org/t/pushing-objects-in-to-array/587/6 "2018-06-04T01:27:16Z")

</div>

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):

```auto
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];

```

---

<div class="post-metadata">

**Author:** ![toni](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@toni](https://discourse.nodered.org/u/toni)\
**Post date:** [4 June 2018 06:14 UTC](https://discourse.nodered.org/t/pushing-objects-in-to-array/587/7 "2018-06-04T06:14:22Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 June 2018 07:57 UTC](https://discourse.nodered.org/t/pushing-objects-in-to-array/587/8 "2018-06-04T07:57:43Z")

</div>

> [@toni](#):
>
> Would using new testObject have changed anything

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