Created
July 14, 2020 16:46
-
-
Save phillipharding/0bbc48e40ee646c1cffe793605473271 to your computer and use it in GitHub Desktop.
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
json = [ | |
{ | |
key: 1, | |
id: 1, | |
label: "Parent #1", | |
isexpanded: false, | |
children: [ | |
{ | |
key: 11, | |
id: 11, | |
label: "Child #1-#1", | |
isexpanded: false, | |
children: [ | |
{ | |
key: 111, | |
id: 111, | |
label: "Child #1-#1-#1", | |
isexpanded: false, | |
children: [ | |
] | |
} | |
] | |
} | |
] | |
}, | |
{ | |
key: 2, | |
id: 2, | |
label: "Parent #2", | |
isexpanded: false, | |
children: [ | |
{ | |
key: 22, | |
id: 22, | |
label: "Parent #2-#2", | |
isexpanded: false, | |
children: [ | |
{ | |
key: 222, | |
id: 222, | |
label: "Parent #2-#2-#2", | |
isexpanded: false, | |
children: [ | |
{ | |
key: 2222, | |
id: 2222, | |
label: "Parent #2-#2-#2-#2", | |
isexpanded: false, | |
children: [ | |
] | |
} | |
] | |
} | |
] | |
} | |
] | |
}, | |
{ | |
key: 3, | |
id: 3, | |
label: "Parent #3", | |
isexpanded: false, | |
children: [ | |
] | |
} | |
] | |
function findTreeItem(items, idToFind) { | |
for(let idx = 0; idx < items.length; idx++) { | |
if (items[idx].id === idToFind) { | |
return items[idx]; | |
} | |
if ((items[idx].children) && (items[idx].children.length)) { | |
const foundChild = findTreeItem(items[idx].children, idToFind); | |
if (foundChild) { | |
return foundChild; | |
} | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment