Last active
April 29, 2016 17:17
-
-
Save mlms13/be922e75aaebc8da6045b2d6c4a03560 to your computer and use it in GitHub Desktop.
Collect a flat array of objects as nested map
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
var flat = [ | |
{ id: "A", name: "A", parent: "" }, | |
{ id: "B", name: "B", parent: "" }, | |
{ id: "C", name: "C", parent: "D" }, | |
{ id: "D", name: "D", parent: "B" }, | |
{ id: "E", name: "E", parent: "A" } | |
]; | |
function unflatten(list) { | |
return; //... | |
} | |
unflatten(flat); // should return: | |
var desiredOut = { | |
A: { | |
id: "A", | |
name: "A", | |
children: { | |
E: { | |
id: "E", | |
name: "E", | |
children: {} | |
} | |
} | |
}, | |
B: { | |
id: "B", | |
name: "B", | |
children: { | |
D: { | |
id: "D", | |
name: "D", | |
children: { | |
C: { | |
id: "C", | |
name: "C", | |
children: {} | |
} | |
} | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment