Skip to content

Instantly share code, notes, and snippets.

@rxluz
Last active January 18, 2019 02:35
Show Gist options
  • Save rxluz/651909d08197c2ea7762f1caee43daed to your computer and use it in GitHub Desktop.
Save rxluz/651909d08197c2ea7762f1caee43daed to your computer and use it in GitHub Desktop.
The problem with Object.freeze, see more at: https://medium.com/p/1a258c160c07
const navigateObj = (obj, level = 0) => {
const methodsNamesFromFirstLevel = Object.getOwnPropertyNames(obj);
methodsNamesFromFirstLevel.forEach(prop => {
console.log(`Prop: ${prop} from level ${level}`);
if (typeof obj[prop] === "object") {
navigateObj(obj[prop], ++level);
}
});
};
navigateObj({
obj: {
with: { a: { lot: "of", different: "levels" }, and: { other: "types" } },
sub: "structures",
},
added: "to",
a: { long: "navigation" },
});
/*
Will return:
Prop: obj from level 0
Prop: with from level 1
Prop: a from level 2
Prop: lot from level 3
Prop: different from level 3
Prop: and from level 3
Prop: other from level 4
Prop: sub from level 2
Prop: added from level 1
Prop: a from level 1
Prop: long from level 2*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment