function findIndex(id) {
try {
treeData.forEach((item, index) => {
var child = item;
while (true) {
if (
child.id === id) {
throw index;
}
if (typeof child.children !== 'object') {
break;
}
child = child.children;
}
});
} catch (index) {
return index;
}
return -1;
}
function getStruct(index) {
var child = treeData[index];
var ids = [];
while (true) {
if (typeof
child.id === 'number') {
ids.push(
child.id);
}
if (typeof child.children !== 'object') {
break;
}
child = child.children;
}
return ids;
}
function test(id) {
var index = findIndex(id);
var ids = getStruct(index);
console.log(id + ' ' + ids.join('-'));
}
test(1);
test(2);
test(6);
/*
1 1-2-3
2 1-2-3
6 4-5-6
*/