# Create array of objects in a change node?

**URL:** <https://discourse.nodered.org/t/create-array-of-objects-in-a-change-node/47497>\
**Category:** General\
**Created:** [22 June 2021 19:43 UTC](https://discourse.nodered.org/t/create-array-of-objects-in-a-change-node/47497 "2021-06-22T19:43:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [22 June 2021 19:43 UTC](https://discourse.nodered.org/t/create-array-of-objects-in-a-change-node/47497/1 "2021-06-22T19:43:56Z")

</div>

Trying to create/populate an array of objects via a change node... to avoid creating a function node and line code to do same. I at times need to prime an array, say initialize array that is sent to a table, that is later updated for example.

I know how to create an object via a change node but I have not cracked how to create an array of objects via change node. This has got to be easy... just have not figured the trick out as yet.

```auto
[{"id":"8b0c8ba2.f5e33","type":"inject","z":"332b4aa4.d68ec6","name":"Set Sonic 17,18","props":[{"p":"topic","vt":"str"},{"p":"payload"},{"p":"payload.trigger","v":"17","vt":"num"},{"p":"payload.echo","v":"18","vt":"num"},{"p":"payload.temperature","v":"20.0","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"command/irrigation/TestListener/set/sonic/0","payload":"{}","payloadType":"json","x":180,"y":1620,"wires":[["69ba475c.3d234"]]},{"id":"b46851af.47c1b","type":"inject","z":"332b4aa4.d68ec6","name":"Set Sonic Sensors .0 17,18","props":[{"p":"topic","vt":"str"},{"p":"payload"},{"p":"payload.0","v":"[]","vt":"json"},{"p":"payload.0.trigger","v":"17","vt":"num"},{"p":"payload.0.echo","v":"18","vt":"num"},{"p":"payload.0.temperature","v":"20.0","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"command/irrigation/TestListener/set/sonic","payload":"[]","payloadType":"json","x":160,"y":1660,"wires":[["69ba475c.3d234"]]},{"id":"5819d158.5a28e8","type":"inject","z":"332b4aa4.d68ec6","name":"Set Sonic Sensors [0] 17,18","props":[{"p":"topic","vt":"str"},{"p":"payload"},{"p":"payload[0]","v":"[]","vt":"json"},{"p":"payload[0].trigger","v":"17","vt":"num"},{"p":"payload[0].echo","v":"18","vt":"num"},{"p":"payload[0].temperature","v":"20.0","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"command/irrigation/TestListener/set/sonic","payload":"[]","payloadType":"json","x":160,"y":1700,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [22 June 2021 19:58 UTC](https://discourse.nodered.org/t/create-array-of-objects-in-a-change-node/47497/2 "2021-06-22T19:58:44Z")

</div>

Here is a function node that creates a small array, an example of what I want to do via a change node.

```auto
[{"id":"b46851af.47c1b","type":"inject","z":"332b4aa4.d68ec6","name":"Set Sonic Sensors 0,1,2","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"command/irrigation/TestListener/set/sonic","payload":"","payloadType":"date","x":190,"y":1660,"wires":[["cfc0ea85.3fa0e"]]},{"id":"cfc0ea85.3fa0e","type":"function","z":"332b4aa4.d68ec6","name":"0,1,2","func":"const SENSORS=[\n { trigger: 17, echo: 18, temperature: 20.0 },\n { trigger: 19, echo: 20, temperature: 20.0 },\n { trigger: 21, echo: 22, temperature: 20.0 }\n];\n\nmsg.payload=SENSORS;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":1660,"wires":[["c6fcca0.a2829b8"]]}]

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [22 June 2021 20:10 UTC](https://discourse.nodered.org/t/create-array-of-objects-in-a-change-node/47497/3 "2021-06-22T20:10:25Z")

</div>

you can set the Change node to Json with the diffrence that you have to surround the object properties with double quotes.

```auto
[{"id":"b46851af.47c1b","type":"inject","z":"af25975e21b17ff4","name":"Set Sonic Sensors 0,1,2","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"command/irrigation/TestListener/set/sonic","payloadType":"date","x":410,"y":1320,"wires":[["cfc0ea85.3fa0e","04b082de14c0891b"]]},{"id":"cfc0ea85.3fa0e","type":"function","z":"af25975e21b17ff4","name":"0,1,2","func":"const SENSORS = [\n { trigger: 17, echo: 18, temperature: 20.0 },\n { trigger: 19, echo: 20, temperature: 20.0 },\n { trigger: 21, echo: 22, temperature: 20.0 }\n];\n\nmsg.payload = SENSORS;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":610,"y":1280,"wires":[["ecb3a10dbce136d3"]]},{"id":"ecb3a10dbce136d3","type":"debug","z":"af25975e21b17ff4","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":1280,"wires":[]},{"id":"04b082de14c0891b","type":"change","z":"af25975e21b17ff4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"trigger\":17,\"echo\":18,\"temperature\":20},{\"trigger\":19,\"echo\":20,\"temperature\":20},{\"trigger\":21,\"echo\":22,\"temperature\":20}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":1360,"wires":[["fa50462707e5c385"]]},{"id":"fa50462707e5c385","type":"debug","z":"af25975e21b17ff4","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":1360,"wires":[]}]

```

---

<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:** [21 August 2021 20:10 UTC](https://discourse.nodered.org/t/create-array-of-objects-in-a-change-node/47497/4 "2021-08-21T20:10:30Z")

</div>

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