我让服务器传回这个 JSON,但我不确定如何遍历 Handlebars 中的二维数组。
"userSurvey":[[1],[2],[3]]
我知道要使用{{#each userSurvey}}
,但是我将如何处理usersurvey
对象内部的数组?
我让服务器传回这个 JSON,但我不确定如何遍历 Handlebars 中的二维数组。
"userSurvey":[[1],[2],[3]]
我知道要使用{{#each userSurvey}}
,但是我将如何处理usersurvey
对象内部的数组?
您必须循环 2 次:
{{#each userSurvey}}
{{#each this}}
{{ this }}
{{/each}}
{{/each}}
在这种特殊情况下,如果您只想渲染“123”,您可以这样做:
{{#each userSurvey}}
{{this.[0]}}
{{/each}}
甚至更简单,因为数组会自动转换为字符串:
{{#each userSurvey}}
{{this}}
{{/each}}
{{#each Arr}}
{{#each this}}
<label>{{this.[0]}}</label> {{this.[1]}}<br>
{{/each}}
{{/each}}
这是我循环数组数组的简单示例:)
对 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