javascript create tree structure with parent and child ids code example

Example: implement tree structure with parent id javascript

const data = [
  { id: 56, parentId: 62 },
  { id: 81, parentId: 80 },
  { id: 74, parentId: null },
  { id: 76, parentId: 80 },
  { id: 63, parentId: 62 },
  { id: 86, parentId: 74 },
];
const idMapping = data.reduce((acc, el, i) => {
  acc[el.id] = i;
  return acc;
}, {});

let root = [];
data.forEach(el => {
  // Handle the root element
  if (el.parentId === null || !idMapping[el.parentId]) {
	root.push(el);
  } else {
    // Use our mapping to locate the parent element in our data array
    const parentEl = data[idMapping[el.parentId]];
    // Add our current el to its parent's `children` array
    parentEl.children = [...(parentEl.children || []), el];
  }
});

console.log(root);