Html 在 Handlebars.js 中循环遍历多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15464042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:36:10  来源:igfitidea点击:

Looping through a multidimensional array in Handlebars.js

javascripthtmlhandlebars.js

提问by user1179321

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

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

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

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

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

回答by Simon Boudrias

You'd have to loop 2 times:

你必须循环2次:

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

回答by Seva Arkhangelskiy

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

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

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

Or even simpler, because arrays automatiaclly transform to strings:

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

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

回答by Dream Castle

    {{#each Arr}}
        {{#each this}}
            <label>{{this.[0]}}</label> {{this.[1]}}<br>
        {{/each}}
    {{/each}}

Here is my simple example to loop array of my arrays :)

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

回答by Sukrit Anand

Use the #with helper for the obj

对 obj 使用 #with 助手

{ catg: [ 'java', 'c', 'c++' ],
  quesarray: [ [ 2, 1 ], [ 0, 2, 10, 5, 11, 12 ], [ 7, 5, 3, 8, 0 ] ],
  _id: 5d778d52d410984dc4e3e278,
  username: '[email protected]' }

if you want to access the array quesarray index wise do this

如果你想访问数组 quesarray 索引明智地这样做

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

and output will be 7, 5, 3, 8, 0

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