Data:
var data = [
{
\"id\": 1,
\"level\": \"1\",
\"text\": \"Sammy\",
\"type\": \"Item\",
\"items\": [
Modified Роман Парадеев answer to make it somewhat more dynamic.
function flatten(xs, childSelector) {
return xs.reduce((acc, x) => {
acc = acc.concat(x);
let children = childSelector(x);
if (children) {
acc = acc.concat(flatten(children, childSelector));
}
return acc;
}, []);
}
Now items
is not hardcoded, and you can use it flatten(data, x => x.items)
.