Created
September 23, 2019 22:56
-
-
Save IhsanMujdeci/eef7d7af69e120541b595b8cd682a10f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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