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

标签 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",

      } 
      ]    
    }
 ]

最佳答案

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

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

关于javascript - 在 Javascript 中对嵌套数组进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45006501/

相关文章:

javascript - 如何根据用户选择进行图像缩放

javascript - 通过角色 :style 设置谷歌条形图的颜色

javascript - 为什么 'arr.values()' 抛出错误 "arr.values is not a function"

php - json_encode() 的问题

javascript - 在 jquery 插件中添加简单公共(public)方法的最佳模式是什么?

javascript - jQuery 在对话框中重置表单验证

Javascript 获取星期几范围数字数组,在两个数字之间,符合 ISOWeekday

javascript从for循环创建数组

json - 网站创建失败,找不到服务器场 Azure Powershell

PHP:json 解码限制