JSFiddle - React, Tailwind, and code Playground
JavaScript
var data = [
{'k':'01'} ,
{'k':'02',
'children':[
{'k':'05'},
{'k':'06',
'children':[
{'k':'ABC'},
{'k':'PQR'}
]
},
{'k':'07'}
]
},
{'k':'03'}
];
function mergeChildren(sources) {
var children = [];
for (var index in sources) {
var source = sources[index];
children.push({k: source.k});
if (source.children) {
children = children.concat(mergeChildren(source.children))
}
}
return children;
}
function findChildrenForK(sources, k) {
for (var index in sources) {
var source = sources[index];
if (source.k === k) {
if (source.children) {
return mergeChildren(source.children);
}
}
}
}
console.log(findChildrenForK(data, '02'))