Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate nested object keys with child nested

I want to iterate the nested object keys which will be have inner child nested as well:

Nested object code:

{
    "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 code to iterate:

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

Actually, the above code working for one nested level to get key(ids). If it reaches the empty object key then the loop is ending there. actually, it should check any other child/next is there or not.

Expected output:

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

Please give me the solution for this.

like image 653
bagya Avatar asked Dec 17 '25 13:12

bagya


1 Answers

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)

Original answer... Here's one way to go about it. Use a recursive function along with Object.entries to gather the data. I found that the result was a series of nested arrays of id's - so I flattened them all with join() and then split them again. Finally, filter() helped remove the empty array indexes

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)
like image 62
Kinglish Avatar answered Dec 19 '25 02:12

Kinglish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!