# Get item from Array dynamically

**URL:** <https://discourse.nodered.org/t/get-item-from-array-dynamically/38195>\
**Category:** General\
**Created:** [29 December 2020 10:54 UTC](https://discourse.nodered.org/t/get-item-from-array-dynamically/38195 "2020-12-29T10:54:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcoVg](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@MarcoVg](https://discourse.nodered.org/u/MarcoVg)\
**Post date:** [29 December 2020 10:54 UTC](https://discourse.nodered.org/t/get-item-from-array-dynamically/38195/1 "2020-12-29T10:54:24Z")

</div>

Hello,

I nedd to read from an array of object the row that in my example is (20). I would like to select the row with an external numeric variable from 0 to 237.

 ![NodeRedArray](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/5/85e7c10e118838b21a12116251d98a5bf97dccac.jpeg)

Thanks for the help,

---

<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:** [29 December 2020 11:01 UTC](https://discourse.nodered.org/t/get-item-from-array-dynamically/38195/2 "2020-12-29T11:01:08Z")

</div>

You can pass the number of interest in any `msg` property you want & use a function or change node to grab the item...

in a function...

```auto
msg.item = msg.payload[msg.index];
return msg;

```

or in a change node using jsonata SET `item` TO `payload[index]`

Demo flow to import and try out...

```auto
[{"id":"c1093799.d30798","type":"inject","z":"bec69dbd.8d622","name":"index 6","props":[{"p":"index","v":"6","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":600,"wires":[["400bb107.9c021"]]},{"id":"522288e7.1fc248","type":"debug","z":"bec69dbd.8d622","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"item","statusType":"msg","x":690,"y":600,"wires":[]},{"id":"400bb107.9c021","type":"change","z":"bec69dbd.8d622","name":"get data","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\"string 1\",\"string 2\",\"string 3\",\"string 4\",\"string 5\",\"string 6\",\"string 7\",\"string 8\",\"string 9\",\"string 10\",\"string 11\",\"string 12\",\"string 13\",\"string 14\",\"string 15\",\"string 16\",\"string 17\",\"string 18\",\"string 19\"]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":600,"wires":[["7287c163.e1905"]]},{"id":"1565c0e.702173f","type":"inject","z":"bec69dbd.8d622","name":"index 9","props":[{"p":"index","v":"9","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":640,"wires":[["400bb107.9c021"]]},{"id":"d20e1301.d62e4","type":"inject","z":"bec69dbd.8d622","name":"index 12","props":[{"p":"index","v":"12","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":160,"y":680,"wires":[["400bb107.9c021"]]},{"id":"7287c163.e1905","type":"function","z":"bec69dbd.8d622","name":"get item by index","func":"msg.item = msg.payload[msg.index]\nreturn msg;","outputs":1,"initialize":"","finalize":"","x":510,"y":600,"wires":[["522288e7.1fc248"]]}]

```

---

<div class="post-metadata">

**Author:** ![MarcoVg](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@MarcoVg](https://discourse.nodered.org/u/MarcoVg)\
**Post date:** [29 December 2020 11:12 UTC](https://discourse.nodered.org/t/get-item-from-array-dynamically/38195/3 "2020-12-29T11:12:48Z")

</div>

Thanks for you support Steve.

In my case, how can pass the number of external variable to index ?

To be more clear, I would like to ingect an extern nuber, and select the richt row from my array.

Thanks for the support,

---

<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:** [29 December 2020 11:13 UTC](https://discourse.nodered.org/t/get-item-from-array-dynamically/38195/4 "2020-12-29T11:13:41Z")

</div>

> [@MarcoVg](#):
>
> To be more clear, I would like to ingect an extern nuber, and select the richt row from my array.

thats what my demo flow does.

---

<div class="post-metadata">

**Author:** ![MarcoVg](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@MarcoVg](https://discourse.nodered.org/u/MarcoVg)\
**Post date:** [29 December 2020 11:17 UTC](https://discourse.nodered.org/t/get-item-from-array-dynamically/38195/5 "2020-12-29T11:17:36Z")

</div>

Thanks I read after my last reply..... 😅

---

<div class="post-metadata">

**Author:** ![MarcoVg](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@MarcoVg](https://discourse.nodered.org/u/MarcoVg)\
**Post date:** [29 December 2020 14:34 UTC](https://discourse.nodered.org/t/get-item-from-array-dynamically/38195/6 "2020-12-29T14:34:06Z")

</div>

HI Steve,

If I received the value of index, not from an inject fix value, but from an array in position 0 I try to wite the following code, but nothing happen,,,,

var data=msg.payload;  
var indice=data[0];  
msg.index=indice;

return msg;

---

<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:** [29 December 2020 14:41 UTC](https://discourse.nodered.org/t/get-item-from-array-dynamically/38195/7 "2020-12-29T14:41:44Z")

</div>

You will need to share your flow, you have not provided enough information to help you.

---

<div class="post-metadata">

**Author:** ![MarcoVg](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@MarcoVg](https://discourse.nodered.org/u/MarcoVg)\
**Post date:** [29 December 2020 15:20 UTC](https://discourse.nodered.org/t/get-item-from-array-dynamically/38195/8 "2020-12-29T15:20:28Z")

</div>

I had a mystake about the index max of number of row...  
Thanks a lot for your support,

Regards,

Marco

---

<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:** [27 February 2021 15:20 UTC](https://discourse.nodered.org/t/get-item-from-array-dynamically/38195/9 "2021-02-27T15:20:35Z")

</div>

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