为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

幕布斯7119047 2023-08-10 15:26:12
我尝试使用此对象数组对对象数据进行分组: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;    }, []);
查看完整描述

4 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

我建议你分两次做。


第一次使用分组功能按 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)


查看完整回答
反对 回复 2023-08-10
?
长风秋雁

TA贡献1757条经验 获得超7个赞

编辑以使用reduce。


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

  }

];


let ret = data.reduce((ret, cur) => {

  let computed = ret.find(record => cur.IdUser === record.IdUser)

  if (computed) {

    computed[cur.transferType].push(cur)

  } else {

    let fresh = {

      IdUser: cur.IdUser,

      Name: cur.Name,

      download: [],

      upload: []

    }

    fresh[cur.transferType].push(cur)

    ret.push(fresh)

  }

  return ret

}, [])


console.log(ret)


查看完整回答
反对 回复 2023-08-10
?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

您可以使用该array.map方法。


const myObj = [

    {

       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: 'downloaded', total: 20 }],

       upload: [{ IdUser: 11, Name: "Joe Smith", transferType: 'upload', total: 16 }]

    }

  ];


const uploadData = myObject.map(val => val.upload);

const downloadData = myObject.map(val => val.download);

const newData = uploadData.concat(downloadData);


查看完整回答
反对 回复 2023-08-10
?
慕丝7291255

TA贡献1859条经验 获得超6个赞

您可以使用一个简单的循环,例如:

for (let i=0; i<data.length; i++) { ... }

在循环内部,您可以访问每个对象和属性,例如:

data[i].IdUser


查看完整回答
反对 回复 2023-08-10
  • 4 回答
  • 0 关注
  • 150 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信