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

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

本文关键字:动态 对象 js node pug jade JavaScript      更新时间:2023-09-26

我的视图上有对象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是可变的。有没有简单的解决方案?

sumPrice.grp不起作用,

因为sumPrice没有名为grp的键。

您可以使用 sumPrice[grp] .

在本例中,您将使用 grp 的值,而不是它的名称。