Loop through array of object

Hi,

I have this flow

and its corresponding output is

I need to access the highlighted values. Can anybody help with the function.

Thanks,
Neeta

Hi Neeta,

If you hover your mouse over one of the highlighted entries, you will see some options appear one of which lets you copy the path to the object.

With that in hand, you can, if you need to walk through the array, replace any array references with a looping function.

msg.payload.sensordatavalues.forEach( (val) => {
    // val.value and val.value_type are what you need
})
1 Like

You can also loop through the array using a split node.
e.g.

[{"id":"354197ee.e1a278","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload.sensor_data_values","v":"[{\"value\":\"2:20\",\"data_type\":\"f1\",\"id\":1234},{\"value\":\"3:20\",\"data_type\":\"f2\",\"id\":1235}]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":100,"y":80,"wires":[["fa34fe67.be89c","208ed9c4.f23cce"]]},{"id":"fa34fe67.be89c","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":430,"y":80,"wires":[]},{"id":"208ed9c4.f23cce","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"move","p":"payload.sensor_data_values","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":120,"wires":[["f49a9fd5.5aa04"]]},{"id":"f49a9fd5.5aa04","type":"split","z":"bf9e1e33.030598","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":630,"y":120,"wires":[["c800f9d1.d02bf8"]]},{"id":"c800f9d1.d02bf8","type":"debug","z":"bf9e1e33.030598","name":"do something to each array element","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":120,"wires":[]}]
1 Like

Thanks you so much. worked for me

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