簡體   English   中英

如何使用嵌套的子項迭代嵌套的對象鍵

[英]How to iterate nested object keys with child nested

我想迭代嵌套對象鍵,這些鍵也將嵌套內部子項:

嵌套對象代碼:

{
    "F2C3C496-BEA6-A5E8-15F0-E2867304B463": {
        "D39FD497-9529-6CC3-70DE-E8D9277C18D3": {
            "child": {
                "87A1817D-CFA9-70FD-131B-224658AF13CE": {
                    "next": {
                        "F7A90266-B7F4-4CC2-0F1B-D4B320E2EC1A": {}
                    }
                }
            },
            "next": {
                "B49927ED-369E-E219-FC1A-8E4BAAFC3933": {
                    "next": {}
                }
            }
        }
    }
}

JS代碼進行迭代:

flowThrough = (obj, callback, context?, path?: string) => {
    let nestedKey=';'
    Object.keys(obj).forEach(key => {
      if(!isEmptyObject(key))
      callback(key, context, path);

      if (!isEmpty(obj[key]) && typeof obj[key] === 'object') {
        if(obj[key].hasOwnProperty('next'))
        nestedKey = obj[key].next;
        else if(obj[key].hasOwnProperty('child'))
        nestedKey = obj[key].child;
        else  nestedKey = obj[key];

        this.flowThrough(nestedKey, callback, context, (path)? has(context, path.concat(".next"))?path.concat(`.next[${get(context, path.concat(".next").length)}]`): path.concat('.').concat("next[0]") : key)
      }
    })
  }

實際上,上面的代碼適用於一個嵌套級別以獲取 key(ids)。 如果它到達空對象鍵,則循環在那里結束。 實際上,它應該檢查任何其他孩子/下一個是否存在。

預期輸出:

{  "flow": [
    {
      "id": "F2C3C496-BEA6-A5E8-15F0-E2867304B463",
      "next": [
        {
          "id": "D39FD497-9529-6CC3-70DE-E8D9277C18D3",
          "child": [
            {
              "id": "87A1817D-CFA9-70FD-131B-224658AF13CE",
              "next": [
                {
                  "id": "F7A90266-B7F4-4CC2-0F1B-D4B320E2EC1A",
                  "next": []
                }
              ]
            }
          ],
          "next": [
            {
              "id": "B49927ED-369E-E219-FC1A-8E4BAAFC3933",
              "next": []
            }
          ]
        }
      ]
    }
  ]
}

請給我解決方案。

 let obj = { "F2C3C496-BEA6-A5E8-15F0-E2867304B463": { "D39FD497-9529-6CC3-70DE-E8D9277C18D3": { "child": { "87A1817D-CFA9-70FD-131B-224658AF13CE": { "next": { "F7A90266-B7F4-4CC2-0F1B-D4B320E2EC1A": {} } } }, "next": { "B49927ED-369E-E219-FC1A-8E4BAAFC3933": { "next": {} } } } } } const setObj = e => { let tmp = {} if (Array.isArray(e[0])) e = e[0] if (typeof e[0] === 'string' && e[0].split('-').length === 5) { tmp = { id: e[0], next: setObj(Object.entries(e[1])) }; } else if (e[1]) { tmp = { child: setObj(Object.entries(e[1])) }; } return tmp } let newobj = Object.entries(obj).map(setObj)[0] console.log(newobj)

原始答案...這是一種解決方法。 使用遞歸函數和Object.entries來收集數據。 我發現結果是一系列嵌套的 id 數組 - 所以我用join()它們全部展平,然后再次拆分它們。 最后, filter()幫助刪除了空數組索引

 let obj = { "F2C3C496-BEA6-A5E8-15F0-E2867304B463": { "D39FD497-9529-6CC3-70DE-E8D9277C18D3": { "child": { "87A1817D-CFA9-70FD-131B-224658AF13CE": { "next": { "F7A90266-B7F4-4CC2-0F1B-D4B320E2EC1A": {} } } }, "next": { "B49927ED-369E-E219-FC1A-8E4BAAFC3933": { "next": {} } } } } } const getKeys = e => { let ret = [] if (e[0].split('-').length === 5) ret.push(e[0]); if (e[1]) ret.push(Object.entries(e[1]).map(getKeys)) return ret.join(',') } let keys = Object.entries(obj).map(getKeys)[0].split(",").filter(e => e) console.log(keys)

暫無
暫無

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

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