Skip to content

Instantly share code, notes, and snippets.

@nishanbajracharya
Created June 27, 2020 15:28
Show Gist options
  • Save nishanbajracharya/9d78126c2b56df60a22726e8bd3b5388 to your computer and use it in GitHub Desktop.
Save nishanbajracharya/9d78126c2b56df60a22726e8bd3b5388 to your computer and use it in GitHub Desktop.
// 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