简体   繁体   English

如何在 Jade Pug 中读取 Object?

[英]How te read Object in Jade Pug?

I have this JSON that I send to the PUG page from router.get with the name of prices我有这个 JSON,我从 router.get 发送到 PUG 页面,名称为prices

[{"space2":{"actualPrice":"$ 99.990","oldPrice":"$ 129.990","id":11098071}},{"h20":{"actualPrice":"$ 69.990","oldPrice":"$ 79.990","id":4444311}},{"space":{"actualPrice":"$ 79.990","oldPrice":"$ 99.990","id":4436762}}] [{"space2":{"actualPrice":"$ 99.990","oldPrice":"$ 129.990","id":11098071}},{"h20":{"actualPrice":"$ 69.990","oldPrice ":"$ 79.990","id":4444311}},{"space":{"actualPrice":"$ 79.990","oldPrice":"$ 99.990","id":4436762}}]

and in a pug page I want to show actualPrice of space2 but I dont know how.actualPrice页面中,我想显示space2但我不知道如何。

I tried with prices.space2.actualPrice and in a for cycle and nothing我尝试过prices.space2.actualPrice和 for 循环,什么也没有

It is actually a Javascript question rather than a Pug question:它实际上是一个 Javascript 问题而不是一个 Pug 问题:

-
  const Data = [
    {"space2":{"actualPrice":"$ 99.990","oldPrice":"$ 129.990","id":11098071}},
    {"h20":{"actualPrice":"$ 69.990","oldPrice":"$ 79.990","id":4444311}},
    {"space":{"actualPrice":"$ 79.990","oldPrice":"$ 99.990","id":4436762}}
  ]

p Only space2: 
  each d in Data 
    if d.space2 
      | #{d.space2.actualPrice}

will output:将输出:

Only space2: $99.9990只有空间 2:99.9990 美元


Are you certain you want to structure your data object in this manner?您确定要以这种方式构建数据对象吗? You can access the details of each element of the array only if you know the first property key of each element.只有知道每个元素的第一个属性键,才能访问数组中每个元素的详细信息。

Depending on what your situation is, it might be more useful if it is:根据您的情况,以下情况可能更有用:

  const Data = [
    {"name": "space2", "actualPrice":"$ 99.990","oldPrice":"$ 129.990","id":11098071},
    {"name": "h20", "actualPrice":"$ 69.990","oldPrice":"$ 79.990","id":4444311},
    {"name": "space", "actualPrice":"$ 79.990","oldPrice":"$ 99.990","id":4436762}
  ]

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

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