How do I access an access array item by index in handlebars?

Javascripthandlebars.js

Javascript Problem Overview


I am trying to specify the index of an item in an array within a handlebars template:

{
  people: [
    {"name":"Yehuda Katz"},
    {"name":"Luke"},
    {"name":"Naomi"}
  ]
}

using this:

<ul id="luke_should_be_here">
{{people[1].name}}
</ul>

If the above is not possible, how would I write a helper that could access a spefic item within the array?

Javascript Solutions


Solution 1 - Javascript

Try this:

<ul id="luke_should_be_here">
{{people.1.name}}
</ul>

Solution 2 - Javascript

The following, with an additional dot before the index, works just as expected. Here, the square brackets are optional when the index is followed by another property:

{{people.[1].name}}
{{people.1.name}}

However, the square brackets are required in:

{{#with people.[1]}}
  {{name}}
{{/with}}

In the latter, using the index number without the square brackets would get one:

Error: Parse error on line ...:
...     {{#with people.1}}                
-----------------------^
Expecting 'ID', got 'INTEGER'

As an aside: the brackets are (also) used for segment-literal syntax, to refer to actual identifiers (not index numbers) that would otherwise be invalid. More details in What is a valid identifier?

(Tested with Handlebars in YUI.)

2.xx Update

You can now use the get helper for this:

(get people index)

although if you get an error about index needing to be a string, do:

(get people (concat index ""))

Solution 3 - Javascript

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

Solution 4 - Javascript

If undocumented features aren't your game, the same can be accomplished here:

Handlebars.registerHelper('index_of', function(context,ndx) {
  return context[ndx];
});

Then in a template

{{#index_of this 1}}{{/index_of}}	

I wrote the above before I got a hold of

this.[0]

I can't see one getting too far with handlebars without writing your own helpers.

Solution 5 - Javascript

If you want to use dynamic variables

This won't work:

{{#each obj[key]}}
...
{{/each}}

You need to do:

{{#each (lookup obj key)}}
...
{{/each}}

see handlebars lookup helper and handlebars subexpressions.

Solution 6 - Javascript

Please try this, if you want to fetch first/last.

{{#each list}}

    {{#if @first}}
        <div class="active">
    {{else}}
        <div>
    {{/if}}

{{/each}}


{{#each list}}

    {{#if @last}}
        <div class="last-element">
    {{else}}
        <div>
    {{/if}}

{{/each}}

Solution 7 - Javascript

While you are looping in an array with each and if you want to access another array in the context of the current item you do it like this.

Here is the example data.

[ { name: 'foo', attr: [ 'boo', 'zoo' ] }, { name: 'bar', attr: [ 'far', 'zar' ] } ]

Here is the handlebars to get the first item in attr array.

{{#each player}}
<p> {{this.name}} </p>

{{#with this.attr}} <p> {{this.[0]}} </p> {{/with}}

{{/each}}

This will output

<p> foo </p>
<p> boo </p>

<p> bar </p> <p> far </p>

Solution 8 - Javascript

The following syntax can also be used if the array is not named (just the array is passed to the template):

  <ul id="luke_should_be_here">
  {{this.1.name}}
  </ul>

Solution 9 - Javascript

In my case I wanted to access an array inside a custom helper like so,

{{#ifCond arr.[@index] "foo" }}

Which did not work, but the answer suggested by @julesbou worked.

Working code:

{{#ifCond (lookup arr @index) "" }}

Hope this helps! Cheers.

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
QuestionlukemhView Question on Stackoverflow
Solution 1 - JavascriptdhorriganView Answer on Stackoverflow
Solution 2 - JavascriptArjanView Answer on Stackoverflow
Solution 3 - JavascriptFMQBView Answer on Stackoverflow
Solution 4 - JavascriptBret WeinraubView Answer on Stackoverflow
Solution 5 - JavascriptjulesbouView Answer on Stackoverflow
Solution 6 - Javascriptuser1378423View Answer on Stackoverflow
Solution 7 - JavascriptFatih AcetView Answer on Stackoverflow
Solution 8 - JavascriptJoséView Answer on Stackoverflow
Solution 9 - JavascriptSharukh MastanView Answer on Stackoverflow