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

时间:2017-07-10 07:51:00

标签: javascript arrays json

我有一组用于某些可视化的数据,格式如下



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
      } 
        ]

//although i have tried writing this function but this is not giving me the desired result.


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




我想按周数对数据集进行分组,以便所需的结果应该是这样的。

    [
    { "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",

      } 
      ]    
    }
 ]

5 个答案:

答案 0 :(得分:1)

这是一种分组数据的简洁方法,您应该能够找到如何以此为基础格式化数据作为起点。

grouped = {}

Dataset1.forEach(function(item, index){

    if (!grouped[item.weekcount]) grouped[item.weekcount] = [];
    grouped[item.weekcount].push(item);

});

grouped是一个以周数计算的对象。如果某个周数不作为对象中的键存在,则创建一个空数组,然后将数据推送到该数组。在以后的迭代中,具有相同周数的数据将添加到现有数组中。

答案 1 :(得分:1)

您可以检查从0到最大的每个周数,并过滤您的数组。它可能是这样的:

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 maxWeekCount = 3;
var result = []
for(var i=0; i<maxWeekCount; i++){
  var group = Dataset1.filter(obj => obj.weekcount === i)
  if(group.length) {
    result.push({
      weekCount: i,
      grouped: group
    })
  }
}

console.log(result)

答案 2 :(得分:1)

使用一个辅助对象来维护对weekcount对象的引用,以将数组缩减为分组结构。

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 3 :(得分:1)

Project A to Project A@100%
Project B to Project A@50%
Project C to Project A@75%
 -  -  -  -  -  -  -  -  -  
Project A to Project B@67%
Project B to Project B@100%
Project C to Project B@100%
 -  -  -  -  -  -  -  -  -
Project A to Project C@75%
Project B to Project C@75%
Project C to Project C@100%

通过剥离&#34;键&#34;属性,此解决方案适用于任何输入而无需修改,因此如果您从输入中添加/删除某些属性,它仍将按预期工作,反映更改。

答案 4 :(得分:0)

const formatted = [];

Dataset1.forEach((data) => {
  const { weekcount, comment, commentBy, datestamp } = data;
  let obj = formatted.find((item) => item.weekcount === weekcount);

  if (!obj) {
    formatted.push({
      weekcount,
      grouped: [{
        comment,
        commentBy,
        datestamp
      }]
    })
  } else {
    obj.grouped.push({
      comment,
      commentBy,
      datestamp
    });
  }
});

&#13;
&#13;
const 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
}];

const formatted = [];

Dataset1.forEach((data) => {
  const { weekcount, comment, commentBy, datestamp } = data;
  let obj = formatted.find((item) => item.weekcount === weekcount);

  if (!obj) {
    formatted.push({
      weekcount,
      grouped: [{
        comment,
        commentBy,
        datestamp
      }]
    })
  } else {
    obj.grouped.push({
      comment,
      commentBy,
      datestamp
    });
  }
});

console.log(formatted);
&#13;
&#13;
&#13;