簡體   English   中英

JavaScript node.js中的pug(jade)模板處的動態對象密鑰

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

我的視圖上有對象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的值而不是它的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM