How to get index in Handlebars each helper?

Javascripthandlebars.js

Javascript Problem Overview


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?

<tbody>
	 {{#each item}}
		 <tr>
            <td><!--HOW TO GET ARRAY INDEX HERE?--></td>
			<td>{{this.key}}</td>
			<td>{{this.value}}</td>
	     </tr>
	 {{/each}}
</tbody>

Javascript Solutions


Solution 1 - Javascript

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 : https://github.com/wycats/handlebars.js/issues/250#issuecomment-9514811

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

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

For object iteration, use @key instead:

{{#each object}}
    {{@key}}: {{this}}
{{/each}} 

Solution 2 - Javascript

This has changed in the newer versions of Ember.

For arrays:

{{#each array}}
    {{_view.contentIndex}}: {{this}}
{{/each}}

It looks like the #each block no longer works on objects. My suggestion is to roll your own helper function for it.

Thanks for this tip.

Solution 3 - Javascript

In handlebar version 3.0 onwards,

{{#each users as |user userId|}}
  Id: {{userId}} Name: {{user.name}}
{{/each}}

In this particular example, user will have the same value as the current context and userId will have the index value for the iteration. Refer - http://handlebarsjs.com/block_helpers.html in block helpers section

Solution 4 - Javascript

I know this is too late. But i solved this issue with following Code:

Java Script:

Handlebars.registerHelper('eachData', function(context, options) {
	  var fn = options.fn, inverse = options.inverse, ctx;
	  var ret = "";
	 
	  if(context && context.length > 0) {
	    for(var i=0, j=context.length; i<j; i++) {
	      ctx = Object.create(context[i]);
	      ctx.index = i;
	      ret = ret + fn(ctx);
	    }
	  } else {
	    ret = inverse(this);
	  }
	  return ret;
}); 

HTML:

{{#eachData items}}
 {{index}} // You got here start with 0 index
{{/eachData}}

if you want start your index with 1 you should do following code:

Javascript:

Handlebars.registerHelper('eachData', function(context, options) {
	  var fn = options.fn, inverse = options.inverse, ctx;
	  var ret = "";
	 
	  if(context && context.length > 0) {
	    for(var i=0, j=context.length; i<j; i++) {
	      ctx = Object.create(context[i]);
	      ctx.index = i;
	      ret = ret + fn(ctx);
	    }
	  } else {
	    ret = inverse(this);
	  }
	  return ret;
	}); 

Handlebars.registerHelper("math", function(lvalue, operator, rvalue, options) {
    lvalue = parseFloat(lvalue);
    rvalue = parseFloat(rvalue);
        
    return {
        "+": lvalue + rvalue
    }[operator];
});

HTML:

{{#eachData items}}
     {{math index "+" 1}} // You got here start with 1 index
 {{/eachData}}

Thanks.

Solution 5 - Javascript

Arrays:

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

If you have arrays of objects... you can iterate through the children:

{{#each array}}
    //each this = { key: value, key: value, ...}
    {{#each this}}
        //each key=@key and value=this of child object 
        {{@key}}: {{this}}
        //Or get index number of parent array looping
        {{@../index}}
    {{/each}}
{{/each}}

Objects:

{{#each object}}
    {{@key}}: {{this}}
{{/each}} 

If you have nested objects you can access the key of parent object with {{@../key}}

Solution 6 - Javascript

Using loop in hbs little bit complex

<tbody>
     {{#each item}}
         <tr>
            <td><!--HOW TO GET ARRAY INDEX HERE?--></td>
            <td>{{@index}}</td>
            <td>{{this}}</td>
         </tr>
     {{/each}}
</tbody>

Learn more

Solution 7 - Javascript

In handlebar version 4.0 onwards,

{{#list array}}
  {{@index}} 
{{/list}}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthunderboltzView Question on Stackoverflow
Solution 1 - Javascriptro60View Answer on Stackoverflow
Solution 2 - JavascriptRyan H LewisView Answer on Stackoverflow
Solution 3 - JavascriptEmber FreakView Answer on Stackoverflow
Solution 4 - JavascriptNaitikView Answer on Stackoverflow
Solution 5 - JavascriptRegarBoyView Answer on Stackoverflow
Solution 6 - JavascriptMD SHAYONView Answer on Stackoverflow
Solution 7 - JavascriptanonymousView Answer on Stackoverflow