Home » Html » Looping through a multidimensional array in Handlebars.js

Looping through a multidimensional array in Handlebars.js

Posted by: admin November 29, 2017 Leave a comment

Questions:

I have the server passing back this JSON and I’m not sure how to loop through a 2-dimensional array in Handlebars.

"userSurvey":[[1],[2],[3]]

I know to use {{#each userSurvey}} but then how would I go about do the arrays inside the usersurvey object?

Answers:

You’d have to loop 2 times:

{{#each userSurvey}}
  {{#each this}}
    {{ this }}
  {{/each}}
{{/each}}

Questions:
Answers:

In this particular case if you want to render just “123” you can do this:

{{#each userSurvey}}
    {{this.[0]}}
{{/each}}

Or even simpler, because arrays automatiaclly transform to strings:

{{#each userSurvey}}
    {{this}}
{{/each}}