# Arrays & Objects in Node-RED

**URL:** <https://discourse.nodered.org/t/arrays-objects-in-node-red/5559>\
**Category:** General\
**Created:** [7 December 2018 10:00 UTC](https://discourse.nodered.org/t/arrays-objects-in-node-red/5559 "2018-12-07T10:00:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [7 December 2018 10:00 UTC](https://discourse.nodered.org/t/arrays-objects-in-node-red/5559/1 "2018-12-07T10:00:55Z")

</div>

how can i access to my data in function node.  
i have tested too many things but no one works. exemple  
var matriculeString =msg.payload[0][data3];  
var matriculeString =msg.data3;, etc ....  
 ![im](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e4bd168f891325ad91d5960b39acb6c8b00d0b85.png)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [7 December 2018 10:08 UTC](https://discourse.nodered.org/t/arrays-objects-in-node-red/5559/2 "2018-12-07T10:08:30Z")

</div>

This page in the docs show you how to use the tool-tips in the debug panel to get the path.  
[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

But it would be also worth looking at one of the javascript array and object tutorials online, I still refer to the w3schools pages  
[https://www.w3schools.com/js/js\_arrays.asp](https://www.w3schools.com/js/js_arrays.asp)

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [7 December 2018 10:20 UTC](https://discourse.nodered.org/t/arrays-objects-in-node-red/5559/3 "2018-12-07T10:20:02Z")

</div>

got it. i was missed "" for data3.  
var obj = msg.payload[0];  
var matriculeString =obj["data3"];  
thanks for the pages and for your quick reply.

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [7 December 2018 12:15 UTC](https://discourse.nodered.org/t/arrays-objects-in-node-red/5559/4 "2018-12-07T12:15:12Z")

</div>

i have a counter function node

```
var counter = context.get('counter')|| 0;
msg.count = counter;
      var counter = counter +1;
    context.set('counter',counter);
return msg;

```

is it possible to make a counter inside mustache template node. i have tried this code but does't work. what do i miss her and thank you.  
PS: {{@index}} doesn't show any thing.

> ```
> <html>
> <body>
> <div style= "float: none;display: block;text-align: right;">
> <p><b>Référence : {{payload.1}}</b></p>
> <b>Matricule :</b> {{payload.{{count}}.data3}}
> </div>
> {{# payload}}
> {{data1}} {{data2}} this is index {{@index}}<br>
> {{/ payload}}
> </body>
> </html>
> 
> ```

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [7 December 2018 12:23 UTC](https://discourse.nodered.org/t/arrays-objects-in-node-red/5559/5 "2018-12-07T12:23:26Z")

</div>

Assuming your function is before your template in your flow.  
You could add the counter to the message that is passed along the flow

```auto
var counter = context.get('counter')|| 0;
     msg.counter = counter +1; 
   context.set('counter',msg.counter);
return msg;

```

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [7 December 2018 12:25 UTC](https://discourse.nodered.org/t/arrays-objects-in-node-red/5559/6 "2018-12-07T12:25:48Z")

</div>

yes i did that. my counter function node is working very well. and it is before the template .  
my problem is her :

`{{payload.{{count}}.data3}}`

it doesn't show any thing

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [7 December 2018 12:51 UTC](https://discourse.nodered.org/t/arrays-objects-in-node-red/5559/7 "2018-12-07T12:51:15Z")

</div>

Add a debug node and look for the difference as to where the counter value is compared to your data

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [7 December 2018 12:55 UTC](https://discourse.nodered.org/t/arrays-objects-in-node-red/5559/8 "2018-12-07T12:55:44Z")

</div>

**Référence : }}**

**Matricule :**.data3}} 
 this is what i get in debug node. clearly it doesn't work like that. is there any way else to add counter.

---

<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:** [7 December 2018 13:00 UTC](https://discourse.nodered.org/t/arrays-objects-in-node-red/5559/9 "2018-12-07T13:00:09Z")

</div>

You cannot use mustache tags inside other mustache tags like that.

Mustache is know as 'logic-less' templating - which means it is not meant to include logic within the template. If you need to select a particular element to display out of the `msg.payload` array, you should copy that value to a know property of the message before passing it to the Template.

For example:

```auto
var counter = context.get('counter')|| 0;
msg.count = counter;
counter = counter +1;
context.set('counter',counter);
msg.selectedItem = msg.payload[msg.count].data3
return msg;

```

And then use `{{msg.selectedItem}}` in your template.

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [7 December 2018 14:18 UTC](https://discourse.nodered.org/t/arrays-objects-in-node-red/5559/10 "2018-12-07T14:18:22Z")

</div>

> [@knolleary](#):
>
> {{msg.selectedItem}}

you mean {{selectedItem}}. thank you very much it works perfectly.
