Can get the element of the array for the value of node change?

Hi,
I try to get value of the array as the value for the variable setted by a change node,
but regardless of setting below, I can't get the element of the array:

  • msg.payload[msg.i]
  • msg.payload & '[' & msg.i & ']'

If possible please tell me. Thanks!

msg : Object
  payload: array[13]
  i: 0

Try using the expression: $.payload[$$.i]

1 Like

I tried but the result is elem = array [13],
not the value of the i element of the array.
Also, I tried the following patterns also don't work:

$.payload[$.i]
$.payload[msg.i]
$.payload[i]

well, it works for me:

[{"id":"7083de3b.2396e","type":"inject","z":"2ea8e5.34bdc71c","name":"","props":[{"p":"payload"},{"p":"i","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"A\",\"B\",\"C\",\"D\"]","payloadType":"json","x":250,"y":440,"wires":[["54ab547f.7448fc"]]},{"id":"54ab547f.7448fc","type":"change","z":"2ea8e5.34bdc71c","name":"","rules":[{"t":"set","p":"elem","pt":"msg","to":"$.payload[$$.i]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":440,"wires":[["435214cf.b1c6fc"]]},{"id":"435214cf.b1c6fc","type":"debug","z":"2ea8e5.34bdc71c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"elem","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":440,"wires":[]}]
1 Like

The way in your debug array display value is a little different,
perhaps this is the cause: :roll_eyes:

Ah but also not, I tried the output array of payload based on your sample,
the result is still correct: :thinking:

mode-change-2

[{"id":"4a376c99.1ad434","type":"function","z":"b3e29f7d.79d6e","name":"","func":"var arrElem = [];\n\nfor (var elem of msg.payload) {\n    arrElem.push(elem);\n}\n\nmsg.payload = arrElem;\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","x":660,"y":1620,"wires":[["bca0af6a.32be5"],[]]},{"id":"fe5c0e84.c2107","type":"inject","z":"b3e29f7d.79d6e","name":"","props":[{"p":"payload"},{"p":"i","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"I\",\"J\",\"K\",\"L\"]","payloadType":"json","x":519.8958129882812,"y":1621.2222900390625,"wires":[["4a376c99.1ad434"]]},{"id":"bca0af6a.32be5","type":"change","z":"b3e29f7d.79d6e","name":"","rules":[{"t":"set","p":"elem","pt":"msg","to":"$.payload[$$.i]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":1620,"wires":[["81a8e387.271d9"]]},{"id":"81a8e387.271d9","type":"debug","z":"b3e29f7d.79d6e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":990,"y":1620,"wires":[]}]

I will confirm my flow again.
Thank you very much!

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