# Array Index with flow variable

**URL:** <https://discourse.nodered.org/t/array-index-with-flow-variable/53731>\
**Category:** General\
**Created:** [14 November 2021 18:12 UTC](https://discourse.nodered.org/t/array-index-with-flow-variable/53731 "2021-11-14T18:12:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hyxion14](https://avatars.discourse-cdn.com/v4/letter/h/977dab/32.png) [@Hyxion14](https://discourse.nodered.org/u/Hyxion14)\
**Post date:** [14 November 2021 18:12 UTC](https://discourse.nodered.org/t/array-index-with-flow-variable/53731/1 "2021-11-14T18:12:35Z")

</div>

Hi, I am trying to make a flow to read a JSON and write it to a csv file.

The JSON has an object that contains an array and what I am trying to do is to make a loop to write each object of the array in a line of the file.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/1813a2548e4090755f792bccb05d0952ee092bf6.png)

I need to extract in each execution of the node, the data: msg.payload.data.dataList[index].oltId

If in index, I put the value 0, it works correctly, but I want that value to change to extract all the array objects.

The function that I have created has the code:

```auto
var listaItems = flow.get('listaItems');

var estado = msg.payload.status;
var api = msg.payload.data.api;
var oltId = msg.payload.data.dataList[listaItems].oltId;
var oltName = msg.payload.data.dataList[listaItems].configuration.oltName;
var oltIP = msg.payload.data.dataList[listaItems].configuration.oltIP;

listaItems --;

flow.set('listaItems',listaItems);

msg.payload.estado = estado;
msg.payload.api = api;
msg.payload.oltId = oltId;
msg.payload.oltName = oltName;
msg.payload.oltIP = oltIP;

return msg;

```

The error I have is: TypeError: Cannot read property 'oltId' of undefined.

If instead of doing it that way, I simply put a number in the index, it works correctly:

```auto
var estado = msg.payload.status;
var api = msg.payload.data.api;
var oltId = msg.payload.data.dataList[0].oltId;
var oltName = msg.payload.data.dataList[0].configuration.oltName;
var oltIP = msg.payload.data.dataList[0].configuration.oltIP;

msg.payload.estado = estado;
msg.payload.api = api;
msg.payload.oltId = oltId;
msg.payload.oltName = oltName;
msg.payload.oltIP = oltIP;

return msg;

```

How can I use a variable in the array index?.

Thank you very much

---

<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:** [14 November 2021 19:15 UTC](https://discourse.nodered.org/t/array-index-with-flow-variable/53731/2 "2021-11-14T19:15:22Z")

</div>

Your issue is listitems is 8. ( I am presuming this is what you are using to start your loop. as there is plenty of missing info)  
The array starts at 0 and ends at 7  
So in your first loop it cann ot read  
`msg.payload.data.listItems[8].oltId` as it does not exist.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 November 2021 20:20 UTC](https://discourse.nodered.org/t/array-index-with-flow-variable/53731/3 "2021-11-14T20:20:06Z")

</div>

> [@Hyxion14](#):
>
> a flow to read a JSON

Can I just point out that it is not JSON it is a javascript object. JSON is always a string.

[This post](https://dev.to/desoga/javascript-object-vs-json-demystified-494j) should help to clarify the difference between Javascript objects and JSON.

---

<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:** [8 January 2022 20:03 UTC](https://discourse.nodered.org/t/array-index-with-flow-variable/53731/4 "2022-01-08T20:03:21Z")

</div>

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