data.forEach(d => { d.children = data.filter(dt => dt.parent_code === d.code); if(!d.parent_code) { parents.push(d); } });