Skip to content

Instantly share code, notes, and snippets.

@SubZane
Last active March 3, 2020 12:16
Show Gist options
  • Save SubZane/091e7401103aa65cf244847129a10ed4 to your computer and use it in GitHub Desktop.
Save SubZane/091e7401103aa65cf244847129a10ed4 to your computer and use it in GitHub Desktop.
find key in nested json object
function findNode(object, id) {
const foundObj = object.filter(p => p.id === id)
if (foundObj.length > 0) {
return {...foundObj};
} else {
const oc = object.filter(p => p.hasOwnProperty('subnodes'))
if (oc && typeof oc === 'object' && oc !== null) {
for (var i=0; i < oc.length; i++) {
const retObj = findNode(oc[i].subnodes, id)
if (retObj != null) {
return retObj
}
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment