Created
February 17, 2019 18:37
-
-
Save kesava/c5f2ea94783cbab4065c7c234346a69a to your computer and use it in GitHub Desktop.
Notate Depth in JS
This file contains hidden or 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
var obj = {"isbn": "123-456-222", | |
"author": | |
{ | |
"lastname": "Doe", | |
"firstname": "Jane" | |
}, | |
"editor": | |
{ | |
"lastname": "Smith", | |
"firstname": "Jane" | |
}, | |
"title": "The Ultimate Database Study Guide", | |
"category": { | |
"age": { | |
"born": "1981", | |
"month": { | |
"name": "Jan" | |
} | |
} | |
} | |
}; | |
const notate_depth = obj => { | |
const nd_internal = (obj, depth) => { | |
let keys = Object.keys(obj); | |
return keys.map(k => { | |
if ((typeof obj[k]) === 'object') { | |
return nd_internal(obj[k], depth+1); | |
} else { | |
return [k, depth]; | |
} | |
}); | |
}; | |
return nd_internal(obj, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment