35

我让服务器传回这个 JSON,但我不确定如何遍历 Handlebars 中的二维数组。

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

我知道要使用{{#each userSurvey}},但是我将如何处理usersurvey对象内部的数组?

4

4 回答 4

75

您必须循环 2 次:

{{#each userSurvey}}
  {{#each this}}
    {{ this }}
  {{/each}}
{{/each}}
于 2013-03-17T17:48:45.580 回答
3

在这种特殊情况下,如果您只想渲染“123”,您可以这样做:

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

甚至更简单,因为数组会自动转换为字符串:

{{#each userSurvey}}
    {{this}}
{{/each}}
于 2014-04-25T13:45:21.377 回答
0
    {{#each Arr}}
        {{#each this}}
            <label>{{this.[0]}}</label> {{this.[1]}}<br>
        {{/each}}
    {{/each}}

这是我循环数组数组的简单示例:)

于 2018-03-08T12:35:45.140 回答
0

对 obj 使用 #with 助手

{ catg: [ 'java', 'c', 'c++' ],
  quesarray: [ [ 2, 1 ], [ 0, 2, 10, 5, 11, 12 ], [ 7, 5, 3, 8, 0 ] ],
  _id: 5d778d52d410984dc4e3e278,
  username: 'student@gmail.com' }

如果您想明智地访问数组 quesarray 索引,请执行此操作

{{#each qset}}
{{#with quesarray}}
{{[2]}}
{{/with}}
{{/each}}

输出将是 7, 5, 3, 8, 0

于 2019-09-11T06:15:06.163 回答