Created
December 17, 2019 07:43
-
-
Save andrIvash/fc742ac8c4762711e4d7200ed53faeea to your computer and use it in GitHub Desktop.
from flat nested array to tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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