如何从对象数组中提取数据

时间:2019-11-19 22:35:09

标签: handlebars.js mustache express-handlebars

我正在从数据库中获取数据,并且由于该数据库包含多个记录集,因此最终输出如下:

 [
      [ { TotalRecords: 11873 } ],
   [
    {
          ProductID: 12394,
          ProductTitle: 'XYZ1'
    },
    {
          ProductID: 14282,
          ProductTitle: 'XYZ2'
    },
    {
          ProductID: 11405,
          ProductTitle: 'XYZ3'
    },
    {
          ProductID: 12467,
          ProductTitle: 'XYZ4'
    }    
  ]
]

我将所有这些数据作为名为products的对象传递到我的视图(车把)。如何在包含ProductIDProductTitle信息的第二个数组周围循环显示在页面上?

1 个答案:

答案 0 :(得分:1)

假设位置已知,您可以通过以下方法遍历第二个数组:

{{#each products.[1]}}
  <p>{{ProductID}}: {{ProductTitle}}</p>
{{/each}}

请注意[1],它指定了索引号。