Skip to content

Instantly share code, notes, and snippets.

@mlms13
Last active April 29, 2016 17:17
Show Gist options
  • Save mlms13/be922e75aaebc8da6045b2d6c4a03560 to your computer and use it in GitHub Desktop.
Save mlms13/be922e75aaebc8da6045b2d6c4a03560 to your computer and use it in GitHub Desktop.
Collect a flat array of objects as nested map
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