Skip to content

Instantly share code, notes, and snippets.

@IhsanMujdeci
Created September 23, 2019 22:56
Show Gist options
  • Save IhsanMujdeci/eef7d7af69e120541b595b8cd682a10f to your computer and use it in GitHub Desktop.
Save IhsanMujdeci/eef7d7af69e120541b595b8cd682a10f to your computer and use it in GitHub Desktop.
const obj = {
a: 1,
deeper:{
val: "lol",
amArr: ['uuh', 'yuh']
},
b:{
c:{
val: 2,
arr: [3,4,5],
fn: ()=>{},
}
},
};
function isObject(o) {
return o instanceof Object;
}
function isArray(o) {
return Array.isArray(o)
}
function traverse(x) {
if (isArray(x)) {
return traverseArray(x)
}
if (isObject(x)) {
return traverseObject(x)
}
}
function traverseArray(arr) {
for(const a of arr){
traverse(a)
}
}
function traverseObject(obj) {
for (const key of Object.keys(obj)) {
traverse(obj[key])
}
}
// usage:
traverse(obj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment