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

**URL:** https://discourse.nodered.org/t/can-get-the-element-of-the-array-for-the-value-of-node-change/33550
**Category:** General
**Created:** [30 September 2020 09:11 UTC](https://discourse.nodered.org/t/can-get-the-element-of-the-array-for-the-value-of-node-change/33550 "2020-09-30T09:11:23Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)
#### Post date: [30 September 2020 09:11 UTC](https://discourse.nodered.org/t/can-get-the-element-of-the-array-for-the-value-of-node-change/33550/1 "2020-09-30T09:11:23Z")

</div>

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

 ![node-change](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/82a21869ff2048e1ff39b35f39f6b798fce314eb.png)

---

<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: [30 September 2020 09:14 UTC](https://discourse.nodered.org/t/can-get-the-element-of-the-array-for-the-value-of-node-change/33550/2 "2020-09-30T09:14:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)
#### Post date: [30 September 2020 21:30 UTC](https://discourse.nodered.org/t/can-get-the-element-of-the-array-for-the-value-of-node-change/33550/3 "2020-09-30T21:30:39Z")

</div>

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:

```auto
$.payload[$.i]
$.payload[msg.i]
$.payload[i]

```

---

<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: [30 September 2020 21:35 UTC](https://discourse.nodered.org/t/can-get-the-element-of-the-array-for-the-value-of-node-change/33550/4 "2020-09-30T21:35:57Z")

</div>

well, it works for me:

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

### Author: ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)
#### Post date: [30 September 2020 21:56 UTC](https://discourse.nodered.org/t/can-get-the-element-of-the-array-for-the-value-of-node-change/33550/5 "2020-09-30T21:56:30Z")

</div>

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

 ![mode-change-1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d52e2a27bbc5240d8b18b43221036b80197ed666.png)

---

<div class="post-metadata">

### Author: ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)
#### Post date: [30 September 2020 22:08 UTC](https://discourse.nodered.org/t/can-get-the-element-of-the-array-for-the-value-of-node-change/33550/6 "2020-09-30T22:08:53Z")

</div>

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

![mode-change-2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/951116eb55654115770c0333c1e75ac49f00956a.png)

```auto
[{"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!

---

<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: [14 October 2020 22:08 UTC](https://discourse.nodered.org/t/can-get-the-element-of-the-array-for-the-value-of-node-change/33550/7 "2020-10-14T22:08:58Z")

</div>

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