I have following type of data
data = [{
"grp" : ["A", "B", "C"],
"val" : [1, 2, 3]
}, {
"grp" : ["A", "B", "D"],
"val" : [2, 3, 4]
}, {
"grp" : ["A", "C", "C"],
"val" : [1, 3, 5]
}, {
"grp" : ["B", "Y", "E"],
"val" : [1, 3, 2]
}
]
I want to make groups like these
groups = {
"A": {
"B": {
"C": [
[1, 2, 3],
[2, 3, 4]
],
"D": [
[1, 2, 3],
[2, 3, 4]
]
},
"C": {
"C": [
[1, 3, 5]
]
}
},
"B": {
"Y": {
"E": [
[1, 3, 2]
]
}
}
}
grp
array can be have maximum of 3 elements.
I tried using underscorejs something like below, however, I ended up creating individual groups, not nested groups.
var groups = _(data).reduce(function(memo, o) {
_(o.groups).each(function(j) {
memo[j] = memo[j] || [ ];
memo[j].push(o);
});
return memo;
}, { });
Would appreciate any idea in the right direction.
See Question&Answers more detail:
os