简体   繁体   English

JavaScript node.js中的pug(jade)模板处的动态对象密钥

[英]Dynamic object key at pug (jade) template in JavaScript node.js

I have object sumPrice on my view and i want to display value depend of key. 我的视图上有对象sumPrice ,我想显示键值。 But the key must be the same as the actual value of the grp . 但是密钥必须与grp的实际值相同。

I'll explain better below 我会在下面更好地解释

  - 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
                    // [........]

I suppose in case sumPrice.grp , application searching value for "sumPrice.grp" instead "sumPrice.Fruit" - does not recognize that a grp is variable. 我假设在sumPrice.grp情况下,应用程序搜索“ sumPrice.grp”而不是“ sumPrice.Fruit”的值-无法识别出grp是变量。 Is there any simple solution for this? 有什么简单的解决方案吗?

sumPrice.grp does not work because sumPrice does not have a key named grp . sumPrice.grp不起作用,因为sumPrice没有名为grp的键。

You can do it by using sumPrice[grp] . 您可以使用sumPrice[grp]

In this case, you're going to be using the value of grp and not it's name. 在这种情况下,您将使用grp的值而不是它的名称。

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

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