通过多个键对数组中的对象进行分组

Grouping objects in an array by multiple keys

最初,我有这个数组:

[{
    "vendorid": 1,
    "vendorname": "Vendor1",
    "maxfilelimit": 2,
    "uploadfilename": "Voice1.xlsx"
},
{
    "vendorid": 1,
    "vendorname": "Vendor1",
    "maxfilelimit": 2,
    "uploadfilename": "Ven1_Voice.xlsx"
},
{
    "vendorid": 2,
    "vendorname": "Vendor2",
    "maxfilelimit": 2,
    "uploadfilename": "Voice2.xlsx"
},
{
    "vendorid": 2,
    "vendorname": "Vendor2",
    "maxfilelimit": 2,
    "uploadfilename": "Ven2_Voice.xlsx"
}]

我想要一个没有重复记录的数组中的文件名。所以,我希望输出类似于以下内容:

[{
    "vendorid": 1,
    "vendorname": "Vendor1",
    "maxfilelimit": 2,
    "uploadfilename": ["Voice1.xlsx", "Ven1_Voice.xlsx"]
}, {
    "vendorid": 2,
    "vendorname": "Vendor2",
    "maxfilelimit": 2,
    "uploadfilename": ["Voice2.xlsx", "Ven2_Voice.xlsx"]
}]

我找到了一些解决方案,例如 d3.js、alaSQL,但没有得到预期的输出

您可以使用 array#reduce 并在其中将结果存储在一个对象中,然后使用 Object.values().

提取值

var data = [{"vendorid": 1,"vendorname": "Vendor1","maxfilelimit": 2,"uploadfilename": "Voice1.xlsx"},{"vendorid": 1,"vendorname": "Vendor1","maxfilelimit": 2,"uploadfilename": "Ven1_Voice.xlsx"},{"vendorid": 2,"vendorname": "Vendor2","maxfilelimit": 2,"uploadfilename": "Voice2.xlsx"},{"vendorid": 2,"vendorname": "Vendor2","maxfilelimit": 2,"uploadfilename": "Ven2_Voice.xlsx"}];

var result = data.reduce((hash, obj) => {
  let key = obj.vendorid+'|' +obj.vendorname+'|' +obj.maxfilelimit;
  if(hash[key])
      hash[key].uploadfilename.push(obj.uploadfilename);
  else {
      hash[key] = obj;
      hash[key].uploadfilename = [obj.uploadfilename];
    }
   return hash; 
},{});
console.log(Object.values(result));
.as-console-wrapper { max-height: 100% !important; top: 0; }

var data = [{
    "vendorid": 1,
    "vendorname": "Vendor1",
    "maxfilelimit": 2,
    "uploadfilename": "Voice1.xlsx"
},
{
    "vendorid": 1,
    "vendorname": "Vendor1",
    "maxfilelimit": 2,
    "uploadfilename": "Ven1_Voice.xlsx"
},
{
    "vendorid": 2,
    "vendorname": "Vendor2",
    "maxfilelimit": 2,
    "uploadfilename": "Voice2.xlsx"
},
{
    "vendorid": 2,
    "vendorname": "Vendor2",
    "maxfilelimit": 2,
    "uploadfilename": "Ven2_Voice.xlsx"
}];
;
// create lookup table
let lookup = data.reduce((p, v) => {
    // seen me yet?
    if (p[v.vendorid]) {
    p[v.vendorid].uploadfilename.push(v.uploadfilename); 
  } else {
    // nope ... first time
    p[v.vendorid] = v;
    // create array of filenames
    p[v.vendorid].uploadfilename = [v.uploadfilename];
  }
  return p;
}, {});

// convert back to array
Object.keys(lookup).map(key => lookup[key]);