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.

4
  • "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 Commented Jul 20, 2017 at 13:00
  • Iterate your array through a loop and when the object_Id matches with the other one, merge them. Commented Jul 20, 2017 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. Commented Jul 20, 2017 at 13:06
  • Check stackoverflow.com/questions/19480008/… - using _.union() (provided by underscore or lodash) should do the trick. Commented Jul 20, 2017 at 13:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.