【问题标题】:How te read Object in Jade Pug?如何在 Jade Pug 中读取对象?
【发布时间】:2021-11-02 21:22:22
【问题描述】:

我有这个 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,但我不知道怎么做。

我尝试使用 prices.space2.actualPrice 并在 for 循环中没有任何结果

【问题讨论】:

    标签: javascript arrays json pug jade4j


    【解决方案1】:

    您的问题看起来像是 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}
    

    将输出:

    仅空间 2:99.9990 美元


    您确定要以这种方式构造数据对象吗?如果您知道每个元素的第一个属性键,则可以访问数组中每个元素的详细信息。例如,列出数组中每个项目的actualPrice 将非常困难。

    根据您的情况,如果您的数据按以下方式组织可能会更有用:

    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}
    ]
    

    【讨论】:

      猜你喜欢
      • 2017-06-10
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多