【问题标题】:node.js jade - Unable to access nested array elementsnode.js 玉 - 无法访问嵌套数组元素
【发布时间】:2011-09-06 17:21:35
【问题描述】:

对不起,如果这是一个非常基本的问题,但我找不到与我试图解决的问题类似的任何示例。

谁能解释一下为什么我无法在以下代码中访问嵌套的元素数组,以及如何访问该数组中的元素?从下面的 json 中,我无法获取从第二个结果中找到的“Items”数组。

正在返回以下 json:

{
    "d": {
        "results": [
            {
                "__metadata": {
                    "uri": "...",
                    "type": "..."
                },
                "__index": 1,
                "ID": "someID1",
                "Name": "Some Name 1"
            },
            {
                "__index": 2,
                "Items": [
                    {
                        "__metadata": {
                            "uri": "...",
                            "type": "..."
                        },
                        "ID": "itemID2_1",
                        "Name": "Item 2_1"
                    }
                ]
            },
            {
                "__index": 3,
                "Items": [
                    {
                        "__metadata": {
                            "uri": "...",
                            "type": "..."
                        },
                        "ID": "itemID3_1",
                        "Name": "Item 3_1"
                    }
                ]
            },
            ...

这是玉的布局:

- var results=records, col_type='even';
table#results(style="border-collapse:collapse;")
  tr
    th.result-header Index
    th.result-header ID
    th.result-header Name
  - each r in results
    - col_type=col_type=='even' ? 'odd' : 'even'
    tr.result-row
      - if (!r.Items)
        td(class='result-col-'+col_type,style="border-left:1px solid black")
          #{r.__index}
        td(class='result-col-'+col_type,style="border-left:1px solid black")
          #{r.ID}
        td(class='result-col-'+col_type,style="border-left:1px solid black")
          #{r.Name}
      - else
        td(class='result-col-'+col_type,style="border-left:1px solid black")
          #{r.__index}
        - each i in r.Items
          td(class='result-col-'+col_type,style="border-left:1px solid black")
            #{i.ID}
          td(class='result-col-'+col_type,style="border-left:1px solid black")
            #{i.Name}

【问题讨论】:

    标签: node.js pug


    【解决方案1】:

    这里的问题是你的 JSON 是这种格式

    {
      "d": { 
         "results": [
            ...
          ]
    

    所以你需要在你的翡翠模板中改变这部分

    - each r in results
      - col_type=col_type=='even' ? 'odd' : 'even'
    

    到这里,

    - each r in results['d']['results']
      - col_type=col_type=='even' ? 'odd' : 'even'
    

    这样,您的循环将通过每个数组项。

    【讨论】:

    • 感谢您的回复。我实际上能够迭代结果集(即,“-var results=records”可以解决问题)。我的问题是我无法访问“Items”数组。
    • 嗯,我确实在本地机器上进行了测试。如果我遍历结果,我可以显示所有 3 个项目。修正了我的回答中的一个错字。
    • 啊,records 长什么样子?我认为您需要先eval() 返回的 JSON 字符串。
    • 嗨 Ace,澄清一下,您是使用 javascript/jquery 还是使用 jam 来迭代结果?不管怎样,谢谢你的帮助。
    • 啊这是在jade上使用javascript。
    【解决方案2】:

    所以我遇到了同样的问题。我的解决方案是:

    - each r in results
      - each i in r.Items
         "... do stuff with i"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 2018-05-18
      • 2020-09-15
      • 1970-01-01
      相关资源
      最近更新 更多