首页 > 解决方案 > 如何使用嵌套的子对象迭代嵌套对象键

问题描述

我想迭代嵌套对象键,这些键也将嵌套内部子对象:

嵌套对象代码:

{
    "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": []
            }
          ]
        }
      ]
    }
  ]
}

请给我解决方案。

标签: javascripttypescript

解决方案


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)


推荐阅读