Created
March 5, 2021 07:57
-
-
Save edubskiy/e1749fdab187fe2600ec10ed0417a81c to your computer and use it in GitHub Desktop.
Build tree array from flat array in javascript
This file contains 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 createDataTree = dataset => { | |
const hashTable = Object.create(null); | |
dataset.forEach(aData => hashTable[aData.ID] = {...aData, childNodes: []}); | |
const dataTree = []; | |
dataset.forEach(aData => { | |
if(aData.parentID) hashTable[aData.parentID].childNodes.push(hashTable[aData.ID]) | |
else dataTree.push(hashTable[aData.ID]) | |
}); | |
return dataTree; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment