let index = 0; const mapTree = (org) => { const haveChildren = Array.isArray(org.children) && org.children.length > 0; index++; return { ...org, sortLabel: index, children: haveChildren ? org.children.map(child => mapTree(child)) : null, }; }; const list = listTree.map(org => mapTree(org));