JavaScript node.js中的哈巴狗(jade)模板上的动态对象键

时间:2016-04-25 19:16:46

标签: javascript node.js object pug

我的视图中有对象sumPrice,我希望显示值取决于键。但密钥必须与grp的实际值相同。

我将在下面更好地解释

  - each grp in Object.keys(groupedData)
               h3= grp // for example grp has value Fruit
               table
                  thead
                    th Name
                    th Price
                  tfoot
                    th Sum:
                    th= sumPrice.grp //this doesn't work, but this:  th= sumPrice.Fruit  will work!
                    th
                    th 
                  tbody
                    // [........]

我想假设sumPrice.grp,应用程序搜索值为" sumPrice.grp"相反" sumPrice.Fruit" - 不承认grp是可变的。对此有什么简单的解决方案吗?

1 个答案:

答案 0 :(得分:1)

sumPrice.grp不起作用,因为sumPrice没有名为grp的密钥。

您可以使用sumPrice[grp]来完成此操作。

在这种情况下,您将使用grp的值,而不是它的名称。