Skip to content

Instantly share code, notes, and snippets.

@andrIvash
Created December 17, 2019 07:43
Show Gist options
  • Save andrIvash/fc742ac8c4762711e4d7200ed53faeea to your computer and use it in GitHub Desktop.
Save andrIvash/fc742ac8c4762711e4d7200ed53faeea to your computer and use it in GitHub Desktop.
from flat nested array to tree
const getNestedChildren = (flatArray, parent) => {
const nestedResult = [];
Object.values(flatArray).forEach((item) => {
if (item.parentId === parent) {
const children = getNestedChildren(flatArray, item.id);
if (children.length) {
/* eslint-disable-next-line no-param-reassign */
item.children = children;
}
nestedResult.push(item);
}
});
return nestedResult;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment