在JavaScript中对嵌套数组进行分组

时间:2017-07-20 12:59:51

标签: javascript arrays json database

我的数据是模式 -

(char *) 'h'

我尝试了[Javascript中的组嵌套数组

中提供的以下过程

] 1

{
    "_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")
    }
}

然而,我的担忧并未得到解决。

我希望我的数据以这种方式显示 -

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);

因此_id或projectName不应重复。 请帮帮我。

0 个答案:

没有答案