Last active
August 26, 2020 11:51
-
-
Save leonidkuznetsov18/ebb4d5f860aff6e5cdf783ada9ce734b to your computer and use it in GitHub Desktop.
make tree from flatten array
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 createDataTree = dataset => { | |
| let hashTable = Object.create(null) | |
| dataset.forEach( aData => hashTable[aData.id] = { ...aData, childNodes : [] } ) | |
| let dataTree = [] | |
| dataset.forEach( aData => { | |
| if( Number.isInteger(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