thunderboltz thunderboltz -4 years ago 186
Javascript Question

How to get index in Handlebars each helper?

I'm using Handlebars for templating in my project. Is there a way to get the index of the current iteration of an "each" helper in Handlebars?

{{#each item}}
<td><!--HOW TO GET ARRAY INDEX HERE?--></td>

Answer Source

In the newer versions of Handlebars index (or key in the case of object iteration) is provided by default with the standard each helper.

snippet from :

The index of the current array item has been available for some time now via @index:

{{#each array}}
    {{@index}}: {{this}}

For object iteration, use @key instead:

{{#each object}}
    {{@key}}: {{this}}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download