简体   繁体   English

如何使用把手访问JSON对象中的Array元素

[英]How to access Array elements in JSON object using handlebars

 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; 

Above is the code I used to export JSON object into handlebars. 上面是我用来将JSON对象导出到车把的代码。 I need to access tour details in the tour array in a handlebars file. 我需要在把手文件中的巡回数组中访问巡回详细信息。 How can I access those details to make a list of tour details inside h3 tag. 我该如何访问这些详细信息以在h3标签中列出游览详细信息。 Following is the sample tour.handlebars file. 以下是示例tour.handlebars文件。 Thanks in advance. 提前致谢。

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

You can iterate over your tours array and then use it in handlebars like this: 您可以遍历tours数组,然后在车把中使用它,如下所示:

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

If you are passing data in the template then you can iterate like this: 如果要在模板中传递数据,则可以这样迭代:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM