My data is in the pattern -
{
"_id" : ObjectId("592ea5d8026c1e263c3d99d9"),
"projectName" : "Yosemite",
"events" : {
"eventName" : "Great",
"eventDate" : ISODate("2018-05-30T00:00:00.000+05:30")
}
},
{
"_id" : ObjectId("5954d98f7be20e32842aea16"),
"projectName" : "Alfa Beta",
"events" : {
"eventName" : "Final Review",
"eventDate" : ISODate("2017-07-31T00:00:00.000+05:30")
}
},
{
"_id" : ObjectId("5954dc0023aac8f9f28ec8d2"),
"projectName" : "Hackthon",
"events" : {
"eventName" : "2nd Review",
"eventDate" : ISODate("2017-07-23T00:00:00.000+05:30")
}
},
{
"_id" : ObjectId("5954dc0023aac8f9f28ec8d2"),
"projectName" : "Hackthon",
"events" : {
"eventName" : "Final Review",
"eventDate" : ISODate("2017-07-31T00:00:00.000+05:30")
}
}
I tried the following procedure available in [Group nested array in Javascript
]1
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}];
var helperMap = {};
var result = Dataset1.reduce(function(arr, obj) {
var current = helperMap[obj.weekcount];
if(!current) {
current = {
weekcount: obj.weekcount,
grouped: []
};
helperMap[obj.weekcount] = current;
arr.push(current);
}
current.grouped.push({
commentBy: obj.commentBy,
comment: obj.comment,
datestamp: obj.datestamp
});
return arr;
}, []);
console.log(result);
However, my concern is not resolved.
I want my data to be appear in this way -
{
"_id" : ObjectId("592ea5d8026c1e263c3d99d9"),
"projectName" : "Yosemite",
"events" : {
"eventName" : "Great",
"eventDate" : ISODate("2018-05-30T00:00:00.000+05:30")
}
},
{
"_id" : ObjectId("5954d98f7be20e32842aea16"),
"projectName" : "Alfa Beta",
"events" : {
"eventName" : "Final Review",
"eventDate" : ISODate("2017-07-31T00:00:00.000+05:30")
}
},
{
"_id" : ObjectId("5954dc0023aac8f9f28ec8d2"),
"projectName" : "Hackthon",
"events" : {
"eventName" : "2nd Review",
"eventDate" : ISODate("2017-07-23T00:00:00.000+05:30")
},
{
"eventName" : "Final Review",
"eventDate" : ISODate("2017-07-31T00:00:00.000+05:30")
}
}
So that the _id or projectName should not repeat. Please help me.