Skip to content

Instantly share code, notes, and snippets.

@Rafat97
Last active August 9, 2022 15:57
Show Gist options
  • Save Rafat97/94d495fb7453597ee628d62ebad44c21 to your computer and use it in GitHub Desktop.
Save Rafat97/94d495fb7453597ee628d62ebad44c21 to your computer and use it in GitHub Desktop.
Random Problems

Get all the keys from a deeply nested object

const data = {
  a: "asdsa",
  c: "dsad",
  f: {
    sadasd: "dasd",
    w: "sad",
    m: {
      asd: "sad",
      asdw: {
        w: "das",
        q: ["das"],
      },
    },
  },
  d: "dsad",
};

const objectToKey = (dataGiven) => {
  const keyData = [];
  function cb(dataInp) {
    for (const key in dataInp) {
      const element = dataInp[key];
      keyData.push(key);
      if (element.constructor.name === "Object") {
        cb(element);
      }
    }
  }
  cb(dataGiven);
  return keyData;
};
console.log(objectToKey(data));

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment