Last active
March 3, 2020 12:16
-
-
Save SubZane/091e7401103aa65cf244847129a10ed4 to your computer and use it in GitHub Desktop.
find key in nested json object
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
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