javascript - 如何对数组对象进行分组以获得所需的数据格式?

标签 javascript

我尝试使用此对象数组对对象数据进行分组:

var data = [ 
    { IdUser: 8, Name: "John Smith", transferType: 'download', total: 6 },
    { IdUser: 12, Name: "Jane Smith", transferType: 'download', total: 15 },
    { IdUser: 11, Name: "Joe Smith", transferType: 'download', total: 20 },
    { IdUser: 12, Name: "Jane Smith", transferType: 'upload', total: 7 },
    { IdUser: 11, Name: "Joe Smith", transferType: 'upload', total: 16 },
    { IdUser: 8, Name: "John Smith", transferType: 'upload', total: 12 }
 ];

将所有对象的所需输出格式获取到新数组中:

  [
    {
       IdUser: 8,
       Name: 'John Smith',
       download: [{ IdUser: 8, Name: "John Smith", transferType: 'download', total: 6}],
       upload: [{ IdUser: 8, Name: "John Smith", transferType: 'upload', total: 12 }]
    },
    { 
       IdUser: 12,
       Name: 'Jane Smith',
       donwload: [{ IdUser: 12, Name: "Jane Smith", transferType: 'download', total: 15 }],
       upload: [{ IdUser: 12, Name: "Jane Smith", transferType: 'upload', total: 7 }]
    },
    {
       IdUser: 11,
       Name: 'Joe Smith',
       download: [{ IdUser: 11, Name: "Joe Smith", transferType: 'download', total: 20 }],
       upload: [{ IdUser: 11, Name: "Joe Smith", transferType: 'upload', total: 16 }]
    }
  ];

我尝试使用reduce函数,但它不是我得到的所需格式:

data.reduce(function (a, b) {
      a[b.Name] = a[b.Name] || [];   
      a[b.Name]["IdUser"] = b.IdUser;  
      a[b.Name][b.transferType] = a[b.Name][b.transferType] || [];     
      a[b.Name][b.transferType].push(b);
      return a;
    }, []);

最佳答案

我建议你分两次进行。

第一次使用分组功能按IdUser对数据进行分组。

然后使用 for in 循环:

var data = [
  {
    "IdUser": 8,
    "Name": "John Smith",
    "transferType": "download",
    "total": 6
  },
  {
    "IdUser": 12,
    "Name": "Jane Smith",
    "transferType": "download",
    "total": 15
  },
  {
    "IdUser": 11,
    "Name": "Joe Smith",
    "transferType": "downloaded",
    "total": 20
  },
  {
    "IdUser": 12,
    "Name": "Jane Smith",
    "transferType": "upload",
    "total": 7
  },
  {
    "IdUser": 11,
    "Name": "Joe Smith",
    "transferType": "upload",
    "total": 16
  },
  {
    "IdUser": 8,
    "Name": "John Smith",
    "transferType": "upload",
    "total": 12
  }
];

var groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
};
var tmp =  groupBy(data, 'IdUser');

var formattedData = [];
for(var id in tmp ){
    formattedData.push({
    "IdUser": id,
    "Name": tmp[id][0].Name,
    "download": tmp[id].filter(obj => obj.transferType == "download"),
    "upload":tmp[id].filter(obj => obj.transferType == "upload")
    })
}
console.log(formattedData)

关于javascript - 如何对数组对象进行分组以获得所需的数据格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64898252/

相关文章:

javascript - 我将使用什么正则表达式来获取此链接的特定部分?

javascript - 如何匹配正则表达式中 < & > 之间的任何内容?

javascript - 执行后立即显示子元素

javascript - 在隐藏 ul 之前触发 li 的点击事件

javascript - Apexcharts 工具提示 - 如何为烛台图表添加额外数据?

javascript - Node JS 应用程序不处理 php 邮件程序代码

javascript - jQuery 返回 true 但如果正确则不输入

javascript - Jquery slider 无法多次调用

javascript - React-DatePicker 使用先前选择的日期更新状态

javascript - 查询选择器全部 : manipulating nodes