迭代Handlebars中的对象

时间:2018-06-14 19:37:47

标签: javascript node.js handlebars.js

我有一个看起来像这样的JavaScript对象:

{
    "key1": {
        name: "SomeName1"
    },
    "key2": {
        name: "SomeName2"
    },
    "key3": {
        name: "SomeName3"
    }
}

我将它传递到我的车把模板中。我想在屏幕上显示所有名字。

app.get('/', function(req, res) {
    res.render('index', {
        data: myObject
    });
});

在我的index.hbs文件中,我有:

{{#each data}}
    Name: {{this.name}}
    <br>
{{/each}}

目前,它只是显示

Name:
Name:
Name:

1 个答案:

答案 0 :(得分:1)

让你的代码工作你的对象需要看起来像这样

{
  data:[
    { name: '1'},
    { name: '2'},
    { name: '3'}
  ]
}

或通过像这样的对象键识字

{{#each this}}
   {{#each this}}
         Name: {{this}}
         <br>
   {{/each}}
{{/each}}
相关问题