# How to loop over msg.payload.states as array of objects

**URL:** <https://discourse.nodered.org/t/how-to-loop-over-msg-payload-states-as-array-of-objects/66766>\
**Category:** General\
**Created:** [22 August 2022 15:53 UTC](https://discourse.nodered.org/t/how-to-loop-over-msg-payload-states-as-array-of-objects/66766 "2022-08-22T15:53:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![novski](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/novski/32/23243_2.png) [@novski](https://discourse.nodered.org/u/novski)\
**Post date:** [22 August 2022 15:53 UTC](https://discourse.nodered.org/t/how-to-loop-over-msg-payload-states-as-array-of-objects/66766/1 "2022-08-22T15:53:00Z")

</div>

I have an msg object that has sub array's of objects.

 ![Bildschirmfoto 2022-08-22 um 17.51.51](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/89a1594b5f8e3048294ed414d4947684be87b908.png)

How can I add an element in to the objects?

This is my data: [pastebin link](https://pastebin.com/raw/wxLbtYFm)

The problem is that every time I try to work with msg.payload.portstates (an array of objects in the object) I get errors. I tried a load of loop possibilities like .foreach, for.. in, map, spread operator, but all fails.

My goal i to add the msg.payload.ts (timestamp) in to all the msg.payload.portstates[0-7] as msg.payload.portstates[0].ts

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [22 August 2022 16:05 UTC](https://discourse.nodered.org/t/how-to-loop-over-msg-payload-states-as-array-of-objects/66766/2 "2022-08-22T16:05:08Z")

</div>

In a `function` node, you can modify the sub-array objects, like this:

```auto
msg.payload.portstates.forEach(obj => obj.ts = msg.payload.ts);
return msg;

```

Of course, this includes NO CHECKING that the payload has a `ts` field or value, and it assumes that `portstates` is an array of objects... but you get the idea.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 August 2022 16:29 UTC](https://discourse.nodered.org/t/how-to-loop-over-msg-payload-states-as-array-of-objects/66766/3 "2022-08-22T16:29:09Z")

</div>

try

```auto
msg.payload.portstates = msg.payload.portstates.map(obj => Object.assign(obj, {ts: msg.payload.ts}));
return msg;

```

@shrickus your code would return an array of timestamps, you would need to do

```auto
msg.payload.portstates = msg.payload.portstates.map(obj => {
    obj.ts = msg.payload.ts
    return obj;
    }
);
return msg;

```

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [22 August 2022 16:30 UTC](https://discourse.nodered.org/t/how-to-loop-over-msg-payload-states-as-array-of-objects/66766/4 "2022-08-22T16:30:52Z")

</div>

`Array.forEach( ... )` allows me to modify each object in-place... so no need to create a new array or clone any of its objects.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 August 2022 16:32 UTC](https://discourse.nodered.org/t/how-to-loop-over-msg-payload-states-as-array-of-objects/66766/5 "2022-08-22T16:32:06Z")

</div>

Sorry miss read, did not see forEach , my eyes saw map, doh.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 August 2022 19:08 UTC](https://discourse.nodered.org/t/how-to-loop-over-msg-payload-states-as-array-of-objects/66766/6 "2022-08-22T19:08:39Z")

</div>

If you are not comfortable with JavaScript functions, the low code version is `split` -\> `change` -\> `join`

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/ac310629b84a8293c10d3403710c81c9967791ff.png)

```auto
[{"id":"60f6d31fcf25324f","type":"inject","z":"fc65972c8d3b1d68","name":"","props":[{"p":"ts","v":"","vt":"date"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":650,"y":760,"wires":[["27dc35d881e29dfc"]]},{"id":"27dc35d881e29dfc","type":"function","z":"fc65972c8d3b1d68","name":"demo data","func":"\nreturn {\n \"topic\": \"telemetry\",\n \"payload\": {\n \"type\": \"telemetry\",\n \"portstates\": [\n {\n \"port\": \"1\",\n \"name\": \"M1 Ab\",\n \"state\": 0\n },\n {\n \"port\": \"2\",\n \"name\": \"M1 Auf\",\n \"state\": 0\n },\n {\n \"port\": \"3\",\n \"name\": \"M2 Ab\",\n \"state\": 0\n },\n {\n \"port\": \"4\",\n \"name\": \"M2 Auf\",\n \"state\": 0\n },\n {\n \"port\": \"5\",\n \"name\": \"Putzlicht aktiv\",\n \"state\": 0\n },\n {\n \"port\": \"6\",\n \"name\": \"Putzlicht Toggl\",\n \"state\": 0\n },\n {\n \"port\": \"7\",\n \"name\": \"Output Port\",\n \"state\": 0\n },\n {\n \"port\": \"8\",\n \"name\": \"Output Port\",\n \"state\": 1\n }\n],\n \"inputstates\": [\n {\n \"idx\": 1,\n \"name\": \"EVAK\",\n \"state\": 1\n },\n {\n \"idx\": 2,\n \"name\": \"Putzlicht Status\",\n \"state\": 1\n },\n {\n \"idx\": 3,\n \"name\": \"Input\",\n \"state\": 1\n },\n {\n \"idx\": 4,\n \"name\": \"Input\",\n \"state\": 1\n },\n {\n \"idx\": 5,\n \"name\": \"Input\",\n \"state\": 1\n },\n {\n \"idx\": 6,\n \"name\": \"Input\",\n \"state\": 1\n },\n {\n \"idx\": 7,\n \"name\": \"Input\",\n \"state\": 1\n },\n {\n \"idx\": 8,\n \"name\": \"Input\",\n \"state\": 0\n }\n],\n \"ts\": 1661189762\n },\n \"qos\": 1,\n \"retain\": false,\n \"_msgid\": \"ddf24fa0ad0328c6\",\n \"mqtt\": \"gpio/gpio1/device/telemetry\",\n \"cathegory\": \"gpio\",\n \"device\": \"gpio1\"\n};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":810,"y":760,"wires":[["9a8b3997691ba5aa"]]},{"id":"5c4785435ee998b2","type":"split","z":"fc65972c8d3b1d68","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":810,"y":840,"wires":[["2156f99e74f91ed0"]]},{"id":"9a8b3997691ba5aa","type":"change","z":"fc65972c8d3b1d68","name":"","rules":[{"t":"set","p":"ts","pt":"msg","to":"payload.ts","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload.portstates","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":840,"wires":[["5c4785435ee998b2"]]},{"id":"2156f99e74f91ed0","type":"change","z":"fc65972c8d3b1d68","name":"","rules":[{"t":"set","p":"payload.ts","pt":"msg","to":"ts","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":840,"wires":[["da8b4b3454c90b6f"]]},{"id":"da8b4b3454c90b6f","type":"join","z":"fc65972c8d3b1d68","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":1130,"y":840,"wires":[["3ef927e56718546d"]]},{"id":"3ef927e56718546d","type":"debug","z":"fc65972c8d3b1d68","name":"debug 64","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1120,"y":900,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![novski](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/novski/32/23243_2.png) [@novski](https://discourse.nodered.org/u/novski)\
**Post date:** [22 August 2022 19:12 UTC](https://discourse.nodered.org/t/how-to-loop-over-msg-payload-states-as-array-of-objects/66766/7 "2022-08-22T19:12:37Z")

</div>

> [@shrickus](#):
>
> `msg.payload.portstates.forEach(obj => obj.ts = msg.payload.ts);`

"TypeError: Cannot read properties of undefined (reading 'forEach')"

Why does my code say that?

=\> it turns out the input was an string not json. I had to parse it to json first. (of coarse! it's MQTT...)

Thanks @Steve-Mcl for the detailed testing!

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 August 2022 19:15 UTC](https://discourse.nodered.org/t/how-to-loop-over-msg-payload-states-as-array-of-objects/66766/8 "2022-08-22T19:15:24Z")

</div>

> [@novski](#):
>
> (of coarse! it's MQTT...)

the MQTT node can be set to parse the object for you...

---

<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:** [5 September 2022 19:16 UTC](https://discourse.nodered.org/t/how-to-loop-over-msg-payload-states-as-array-of-objects/66766/9 "2022-09-05T19:16:22Z")

</div>

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