Mustache.js getting current Index of an array

arrays, javascript, mustache

Solution

I'm sorry... I think you need to manually add index:

for (i in Data)
   Data[i].index = i;

and

{{#Data}}
    <li id='{{index}}'>
       {{eventName}}<br />
       {{eventLocName}}<br />
       {{eventLocCity}}, {{eventLocST}}
    </li>
{{/Data}}

Problem

On initial page load, we are loading a large json data object with news feeds. We are using Mustache.js and listing all the new feed headlines which is working great. We have a div area to display the details of the onClick news item. Since we have the data already loaded, I need to pass the index of the current item (array index) to grab the news item we need to get details from. ``` {{#Data}} <li> {{eventName}}<br /> {{eventLocName}}<br /> {{eventLocCity}}, {{eventLocST}} </li> {{/Data}} ``` I would like to add an id to the - with the array index such as ``` {{#Data}} <li id='{{Data.index}}'> {{eventName}}<br /> {{eventLocName}}<br /> {{eventLocCity}}, {{eventLocST}} </li> {{/Data}} ``` What is the syntax to get array index into my Mustache.js template? UPDATED / WORKING Once I get my `results` onto the DOM, I used the following JS to add the '`index`' into each array item. (select answer below code helped) ``` for (i in result) result[i].index = i; ```

Original source

Related problems