Skip to content

Instantly share code, notes, and snippets.

@kesava
Created February 17, 2019 18:37
Show Gist options
  • Save kesava/c5f2ea94783cbab4065c7c234346a69a to your computer and use it in GitHub Desktop.
Save kesava/c5f2ea94783cbab4065c7c234346a69a to your computer and use it in GitHub Desktop.
Notate Depth in JS
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