簡體   English   中英

如何遍歷嵌套數組?

[英]How do I loop through nested arrays?

我在下面有這個結構,我想在不丟失任何對象的情況下遍歷層次結構。

{
  "countries": [
    {
      "name": "Denmark",
      "id": "APA1",
      "children": [
        {
          "name": "Zealand",
          "id": "APA1.1",
          "parentId": "APA1",
          "children": [
            {
              "name": "Copenhagen",
              "id": "APA1.1.1",
              "parentId": "APA1.1",
              "children": [
                {
                  "name": "Dublin",
                  "id": "ANA1",
                  "parentId": "APA1.1.1.1",
                  "hostNames": [
                    {
                      "ip": "20.190.129.1"
                    },
                    {
                      "ip": "20.190.129.2"
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "name": "Jutland",
          "id": "APA1.2",
          "parentId": "APA1",
          "children": [
            {
              "name": "Nordjylland",
              "id": "APA1.2.1",
              "parentId": "APA1.2",
              "children": [
                {
                  "name": "Aalborg",
                  "id": "APA1.2.1.1",
                  "parentId": "APA1.2.1",
                  "children": [
                    {
                      "name": "Risskov",
                      "id": "ANA3",
                      "parentId": "APA1.2.1.1",
                      "hostNames": [
                        {
                          "ip": "40.101.81.146"
                        }
                      ]
                    },
                    {
                      "name": "Brabrand",
                      "id": "ANA4",
                      "parentId": "APA1.2.1.1",
                      "hostNames": [
                        {
                          "ip": "43.203.94.182"
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

我想遍歷層次結構的原因是我想把它變成一個平面結構。 所以基本上我要把每個對象移動到另一個具有我想要的結構的數組中。 我只想知道如何訪問孩子。

想要的結果:

"applicationGroups": [
    {
        "id" : "APA1",
        "name": "Denmark",
    },
    {
        "name": "Zealand",
        "id": "APA1.1",
        "parentId": "APA1"
    },
    {
        "name": "Copenhagen",
        "id": "APA1.1.1",
        "parentId": "APA1.1"
    },
    {
        "name": "Dublin",
        "id": "ANA1",
        "parentId": "APA1.1.1.1"
    },
    {
        "name": "Jutland",
        "id": "APA1.2",
        "parentId": "APA1"
    },
    {
        "name": "Nordjylland",
        "id": "APA1.2.1",
        "parentId": "APA1.2"
    },
    {
        "name": "Aalborg",
        "id": "APA1.2.1.1",
        "parentId": "APA1.2.1"
    },
    {
        "name": "Risskov",
        "id": "ANA3",
        "parentId": "APA1.2.1.1"
    },
    {
        "name": "Brabrand",
        "id": "ANA4",
        "parentId": "APA1.2.1.1"
    }
]

我對 JavaScript 有點陌生,我真的不知道從哪里開始,但是我給出的這個例子與我正在研究的實際例子不同,所以我只想要原理,這樣我就可以在我的實際代碼中自己實現它。

您可以結合使用Array.flat()方法和這個答案來遞歸地展平對象。 使用遞歸函數是實現這一目標的更快方法。

要獲得扁平結構,您可以使用reduce方法來創建遞歸函數。

 const data = {"countries":[{"name":"Denmark","id":"APA1","children":[{"name":"Zealand","id":"APA1.1","parentId":"APA1","children":[{"name":"Copenhagen","id":"APA1.1.1","parentId":"APA1.1","children":[{"name":"Dublin","id":"ANA1","parentId":"APA1.1.1.1","hostNames":[{"ip":"20.190.129.1"},{"ip":"20.190.129.2"}]}]}]},{"name":"Jutland","id":"APA1.2","parentId":"APA1","children":[{"name":"Nordjylland","id":"APA1.2.1","parentId":"APA1.2","children":[{"name":"Aalborg","id":"APA1.2.1.1","parentId":"APA1.2.1","children":[{"name":"Risskov","id":"ANA3","parentId":"APA1.2.1.1","hostNames":[{"ip":"40.101.81.146"}]},{"name":"Brabrand","id":"ANA4","parentId":"APA1.2.1.1","hostNames":[{"ip":"43.203.94.182"}]}]}]}]}]}]} function flat(data) { return data.reduce((r, { children, ...rest }) => { if (children) r.push(...flat(children)) r.push(rest) return r; }, []) } const result = flat(data.countries) console.log(result)

您可以采用flatMap方法進行展平回調的遞歸調用。

 const flat = ({ hostNames, children = [], ...o }) => [o, ...children.flatMap(flat)], data = { countries: [{ name: "Denmark", id: "APA1", children: [{ name: "Zealand", id: "APA1.1", parentId: "APA1", children: [{ name: "Copenhagen", id: "APA1.1.1", parentId: "APA1.1", children: [{ name: "Dublin", id: "ANA1", parentId: "APA1.1.1.1", hostNames: [{ ip: "20.190.129.1" }, { ip: "20.190.129.2" }] }] }] }, { name: "Jutland", id: "APA1.2", parentId: "APA1", children: [{ name: "Nordjylland", id: "APA1.2.1", parentId: "APA1.2", children: [{ name: "Aalborg", id: "APA1.2.1.1", parentId: "APA1.2.1", children: [{ name: "Risskov", id: "ANA3", parentId: "APA1.2.1.1", hostNames: [{ ip: "40.101.81.146" }] }, { name: "Brabrand", id: "ANA4", parentId: "APA1.2.1.1", hostNames: [{ ip: "43.203.94.182" }] }] }] }] }] }] }, result = data.countries.flatMap(flat); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

暫無
暫無

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

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