Skip to content

Instantly share code, notes, and snippets.

@cyberfly
Last active October 14, 2020 05:08
Show Gist options
  • Select an option

  • Save cyberfly/3f5b2ff4ba62385ab2870e482d06568e to your computer and use it in GitHub Desktop.

Select an option

Save cyberfly/3f5b2ff4ba62385ab2870e482d06568e to your computer and use it in GitHub Desktop.
export function recursiveFind(array, key, value) {
var o;
array.some(function iter(a) {
if (a[key] === value) {
o = a;
return true;
}
return Array.isArray(a.children) && a.children.some(iter);
});
return o;
},
const tree = {
"id": 19,
"global_id": "tgmqevfibl",
"name": "kas",
"children": [{
"id": 21,
"global_id": "zachvhfcsu",
"name": "dsaasd",
"children": []
}, {
"id": 22,
"global_id": "gzimxobiiy",
"name": "laki",
"children": [{
"id": 23,
"global_id": "rqplnkvsuj",
"name": "Test mano aku tahu",
"children": [{
"id": 24,
"global_id": "xepoosxich",
"name": "biar",
"children": [{
"id": 25,
"global_id": "khzixevvqc",
"name": "mano aku tahu"
}]
}]
}]
}]
};
let document = recursiveFind(tree, "id", 24);
if (typeof document !== "undefined") {
// do something
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment