My array of data is structured for visualization as shown below:
var Dataset1 = [
{
"commentBy": "saurabh",
"comment": "Testing",
"datestamp": "07/07/2017",
"weekcount": 1
},
{
"commentBy": "raman",
"comment": "Planning",
"datestamp": "07/07/2017",
"weekcount": 1
},
{
"commentBy": "Execution",
"comment": "Alfa Beta",
"datestamp": "07/07/2017",
"weekcount": 2
},
{
"commentBy": "Execution",
"comment": "Zseta Gama",
"datestamp": "07/07/2017",
"weekcount": 2
}
]
// Despite attempting to create a function, the desired result has not been achieved.
var groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
var groubedByTeam = groupBy(Dataset1, 'weekcount')
console.log(groubedByTeam);
In order to group the dataset by weekcount, I wish to see the following result:
[
{
"weekcount": 1,
"grouped": [
{
"commentBy": "saurabh",
"comment": "Testing",
"datestamp": "07/07/2017"
},
{
"commentBy": "raman",
"comment": "Planning",
"datestamp": "07/07/2017"
}
]
},
{
"weekcount": 2,
"grouped": [
{
"commentBy": "Execution",
"comment": "Alfa Beta",
"datestamp": "07/07/2017"
},
{
"commentBy": "Execution",
"comment": "Zseta Gama",
"datestamp": "07/07/2017"
}
]
}
]