【问题标题】:How to access Array elements in JSON object using handlebars如何使用把手访问 JSON 对象中的数组元素
【发布时间】:2017-12-10 21:36:02
【问题描述】:

var express = require('express');
var router = express.Router();

var data = {

    curreny :{
         name:'United States dollars',
         abbrev:'USD'

},
tours:[
  {name: 'Hood River' , price:'100'},
  {name: 'Sri lanka' , price:'150'},
  {name: 'Hood River' , price:'178'}
],
specialsUrl:'/january-specials'


}


router.get('/detour',function(req,res){
    res.render('tours',{object:data});
})
module.exports = router;

上面是我用来将 JSON 对象导出到车把的代码。我需要在车把文件中访问游览数组中的游览详细信息。如何访问这些详细信息以在 h3 标记内列出旅游详细信息。以下是示例 tour.handlebars 文件。提前致谢。

<h2> Hello this is the body section</h2>
<h3>currency : {{object.curreny.name}}</h3>

<h3>tours : {{object.tours.body}}</h3>

【问题讨论】:

    标签: javascript json node.js express handlebars.js


    【解决方案1】:

    您可以遍历 tours 数组,然后在把手中使用它,如下所示:

    {{#each object.tours}}
        <h3>name: {{name}} </h3>
        <h3>price: {{price}} </h3>
    {{/each}}
    

    【讨论】:

      【解决方案2】:

      如果你在模板中传递数据,那么你可以像这样迭代:

      {{#each tours}}
         <h3>name :{{name}}</h3>
      {{/each}}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-30
        • 1970-01-01
        • 2018-05-30
        • 1970-01-01
        • 1970-01-01
        • 2015-05-15
        • 1970-01-01
        • 2020-11-19
        相关资源
        最近更新 更多