const data= [
{
label: 'Level one 1',
children: [
{
label: 'Level two 1-1',
children: [
{
label: 'Level three 1-1-1',
},
],
},
],
},
{
label: 'Level one 2',
children: [
{
label: 'Level two 2-1',
children: [
{
label: 'Level three 2-1-1',
},
],
},
],
}
]
function preOrder(list, result=[], labels=[]) {
return list.reduce((res,v) => {
labels.push(v.label);
if(v.children && v.children.length) preOrder(v.children, res, labels);
else res.push(labels.join(' > '))
labels.pop();
return res;
}, result)
}
console.log(preOrder(data))
console