After analyzing the given data structure:
var data = [{
name: "Some Name",
id: 1,
children: [
{ name: "prop1", value: 1 },
{ name: "prop2", value: 2 },
{ name: "prop3", value: 3 }
]
},
{
name: "Some Other Name",
id: 2,
children: [
{ name: "prop1", value: 4 },
{ name: "prop2", value: 5 },
{ name: "prop3", value: 6 }
]
}];
...with a dynamic list in 'children', I successfully restructured this using nested _.each
loops like below:
_.each(data, d => {
_.each(d.children, c => {
d[c.name] = c.value;
})
});
This resulted in a new 2-dimensional data structure as shown:
[{
name: "Some Name",
id: 1,
prop1: 1,
prop2: 2,
prop3: 3
},
{
name: "Some Other Name",
id: 2,
prop1: 4,
prop2: 5,
prop3: 6
}];
Looking for a more efficient way to achieve this restructuring using undercore.js?
Try it out on JSFiddle: http://jsfiddle.net/3m3dsv47/