Last active
October 14, 2020 05:08
-
-
Save cyberfly/3f5b2ff4ba62385ab2870e482d06568e to your computer and use it in GitHub Desktop.
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
| 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; | |
| }, |
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
| 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