Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through Array of Arrays in Ember

I'm bit new and confused about Ember iterations.

I'm trying to construct a select inside ember template, like:

<select id="state_list">
  {{#each stateArrays as |stateArray|}}
    <option value={{stateArray[0]}}>{{stateArray[1]}}</option>
  {{/each}}
</select>

Here, stateArrays looks like:

[[1, "Alabama"], [2, "Alaska"], [3, "Arizona"]]

But, this throws error. When I try {{stateArray}}, I get string like "1, Albama"...

How to achieve the above in single iteration?

like image 297
Abhi Avatar asked Oct 17 '22 22:10

Abhi


1 Answers

Not that I love this technique, but you can access the individual elements on an array like this

{{#each stateArrays as |stateArray|}}
    <option value={{stateArray.[0]}}>{{stateArray.[1]}}</option>
  {{/each}}

Twiddle

Saves you having to write extra code.

like image 149
JonRed Avatar answered Oct 21 '22 00:10

JonRed