1

I am currently having trouble accessing an element of an array based on the index of another array. Here is what I'm trying to do in a simplified version:

So I have 2 arrays that I pass to handlebars:

array1 = [Top, Mid, Jungle, ADC, Support]
array2 = [Gankplank, Ahri, Khazix, Ezreal, Janna]

Here is my simplified handlebars code:

{{#each array1}}
   {{this}} - {{array2.[@INDEX OF ARRAY 1]}}
{{/each}}

So my desired output would look like:

Top - Gankplank
Mid - Ahri
Jungle - Khazix
ADC - Ezreal
Support - Janna

1
  • Merge this both array in to object and pass it to handlebarJS. refer my answer if you are okay with this approach ! Commented Dec 28, 2015 at 0:23

1 Answer 1

1

Try the below snippets to create object and pass this object to handlebar template.

In Underscore

var tplObj = _.object(['Top', 'Mid', 'Jungle'], ['Gankplank', 'Ahri', 'Khazix']);
// output => {'Top': 'Gankplank', 'Mid': 'Ahri', 'Jungle': 'Khazix'}

In Plain JS

var tplObj = {};
for(var i in array1) {
 tplObj[ array2[i] ] = array1[i];
}

Iterate Object in handlebar template like below

{{#each tplObj}}
    Key: {{@key}} Value = {{this}}
{{/each}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.