Created
June 27, 2020 15:28
-
-
Save nishanbajracharya/9d78126c2b56df60a22726e8bd3b5388 to your computer and use it in GitHub Desktop.
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
// https://gist.github.com/nishanbajracharya/8fe38807b3ad074a7da2072c7b8e701b | |
function normalize(input = {}) { | |
var values = Array.isArray(input) ? input : Object.values(input); | |
return values.reduce((acc, value) => { | |
if (value.children) { | |
return { | |
...acc, | |
[value.id]: { | |
id: value.id, | |
name: value.name, | |
children: value.children.map((child) => child.id), | |
}, | |
...normalize(value.children), | |
}; | |
} | |
return {...acc, [value.id]: value}; | |
}, {}); | |
} | |
var result = normalize(input); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment