JSFiddle - React, Tailwind, and code Playground
HTML
<div id="result"></div>
JavaScript
const descriptor = {
name: "aa",
columns: [
{
title: 'ba',
columns: [
{
title: 'ca',
columns: [
{
title: 'l1',
width: 10
},
{
title: 'l2',
width: 15
},
{
title: 'l3',
width: 20
}
]
},
{
title: 'cb',
columns: [
{
title: 'l3',
width: 10
},
{
title: 'l4',
width: 15
}
]
},
{
title: 'l5',
width: 20
}
]
},
{
title: 'l6',
width: 10
}
]
};
const getWidths = data =>
[].concat(data || []).reduce((acc, n) => {
if (n.hasOwnProperty('width')) {
acc.push(n.width);
}
acc.push(...getWidths(n.columns));
return acc;
}, []);
document.querySelector('#result').textContent = JSON.stringify(getWidths(descriptor));