简体   繁体   中英

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. But the key must be the same as the actual value of the 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. Is there any simple solution for this?

sumPrice.grp does not work because sumPrice does not have a key named grp .

You can do it by using sumPrice[grp] .

In this case, you're going to be using the value of grp and not it's name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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