0

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.

UmarZaii
  • 1,355
  • 1
  • 17
  • 26
Bulla
  • 70
  • 2
  • 9
  • `"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") }` isn't valid. Should be an array – tymeJV Jul 20 '17 at 13:00
  • Iterate your array through a loop and when the object_Id matches with the other one, merge them. – Kamesh Jul 20 '17 at 13:05
  • Even after changing it to an array e.g. "eventDate" : "2018-05-29T18:30:00.000Z", I am not able to get the required data format. Thanks for looking it. – Bulla Jul 20 '17 at 13:06
  • Check https://stackoverflow.com/questions/19480008/javascript-merging-objects-by-id - using `_.union()` (provided by underscore or lodash) should do the trick. – Conan Jul 20 '17 at 13:24

0 Answers0