Home > Net >  Get all the children objects key into new array of objects using reduce
Get all the children objects key into new array of objects using reduce

Time:11-29

Currently I have a object which has parent to child structure , this Child's dependency can increase upto 12-13 levels , Bellow is example parent to child array of object curranty I have

{
"EMSA": {
    "North Europe Region": {
        "Denmark": {
            "a/s": {
                "VC": {
                    "VK Supplier ": {}
                },
                "DM": {
                    "DM Denmark": {}
                },
                "DQ": {
                    "DQ A/S": {}
                }
            }
        }
    },
    "East Europe Region": {
        "India": {
            "a/s India": {
                "VP": {
                    "Vp Supplier": {}
                },
                "DC": {
                    "DC india": {}
                },
                "HQ": {
                    "HQ A\S": {}
                }
            }
        }
    }
}}

I am trying to create a new array of object which contains all the key with a new key assigned to it for example :

{
   "AREA": [
      "EMSA"
   ],
   "SUPER_REGION": [
      "North Europe Region",
      "East Europe Region"
   ],
   "thirdChild": [
      "Denmark",
      "India"
   ],
   "fourthChild": [
      "a/s",
      "a/s India"
   ],
   "fifthchilds": [
      "VK Supplier",
      "DM Denmark",
      "DQ A/S",
      "Vp Supplier",
      "DC india",
      "HQ A\S"
   ]
}

I am trying bellow code so far and i see that res is empty always ,I also see that only get the first layer of keys as output but unable to reach the children layers ,

Bellow is my code :

 const keyify = (obj, prefix ="") =>
  Object.keys(obj).reduce((res, el) => {
    if (typeof obj[el] === "object" && obj[el] !== null) {
      //return [...res, ...keyify(obj[el], prefix   el   ".")];
    }
    return [...res, prefix   el];
  }, []);

keyify(val); //here val is my object similar to 1st obj mentioned 

CodePudding user response:

A simple recursive loop keeping track of how deep in the tree you are will allow you to set the values in your object.

var myObj = {
  "EMSA": {
    "North Europe Region": {
      "Denmark": {
        "a/s": {
          "VC": {
            "VK Supplier ": {}
          },
          "DM": {
            "DM Denmark": {}
          },
          "DQ": {
            "DQ A/S": {}
          }
        }
      }
    },
    "East Europe Region": {
      "India": {
        "a/s India": {
          "VP": {
            "Vp Supplier": {}
          },
          "DC": {
            "DC india": {}
          },
          "HQ": {
            "HQ A\S": {}
          }
        }
      }
    }
  }
};

// put the names of the levels here
const levelKeys = ["AREA", "SUPER_REGION", "three", "four", "five", "six"];

// recursive function that walks the tree
const documentTree = (obj, acc = {}, level = 0) => {
  Object.entries(obj).forEach(([key, value]) => {
    const prop = levelKeys[level] || level;
    acc[prop] = acc[prop] || [];
    acc[prop].push(key);
    // loop over the child object, bump up the level
    documentTree(value, acc, level   1);
  });
  return acc;
}

const res = documentTree(myObj);
console.log(res);

  • Related