javascript - Mustache.js getting current Index of an array -


on initial page load, loading large json data object news feeds. using mustache.js , listing new feed headlines working great.

we have div area display details of onclick news item. since have data loaded, need pass index of current item (array index) grab news item need details from.

{{#data}}     <li>        {{eventname}}<br />        {{eventlocname}}<br />        {{eventloccity}}, {{eventlocst}}     </li> {{/data}} 

i add id

  • array index such as

    {{#data}}     <li id='{{data.index}}'>        {{eventname}}<br />        {{eventlocname}}<br />        {{eventloccity}}, {{eventlocst}}     </li> {{/data}} 

    what syntax array index mustache.js template?

    updated / working

    once results onto dom, used following js add 'index' each array item. (select answer below code helped)

    for (i in result) result[i].index = i; 
  • i'm sorry... think need manually add index:

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

    and

    {{#data}}     <li id='{{index}}'>        {{eventname}}<br />        {{eventlocname}}<br />        {{eventloccity}}, {{eventlocst}}     </li> {{/data}} 

    Comments

    Popular posts from this blog

    php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

    linux - Does gcc have any options to add version info in ELF binary file? -

    java - Are there any classes that implement javax.persistence.Parameter<T>? -