javascript 如何按某些属性值对对象数组进行分组

pn9klfpd  于 2023-03-16  发布在  Java
关注(0)|答案(5)|浏览(229)

我有两个数组。一个是包含名字的字符串数组

let companies = ['Google', 'Coca Cola,' 'Jonson & Jonson',];

另一个数组包含的对象

let employees = [
  {name: 'Alina' company: 'Google', id : 1},
  {name: 'Vika' company: 'Coca Cola', id : 2},
  {name: 'Alex' company: 'Jonson & Jonson', id : 3},
  {name: 'Vlad' company: 'Google', id : 4},
  {name: 'Fibi' company: 'Coca Cola', id : 5},
  {name: 'Joey' company: 'Google', id : 6},
]

我的任务是把这些人按名字分组

const groups = [
 {'Google': [
   {name: 'Alina' company: 'Google', id : 1},
   {name: 'Vlad' company: 'Google', id : 4},
 ]},
 'Jonson & Jonso': [
   {name: 'Alex' company: 'Jonson & Jonson', id : 3},
 ]},
 ...
]

也许有人知道如何做到这一点最简单的方法,而没有额外的迭代JS?我可以使用嵌套循环,但它会太复杂。也许这是可能的做与lodash?也请注意,字符串键的公司名称可能有空格。将非常感谢任何建议。

n3h0vuf2

n3h0vuf21#

一个回到未来的答案:
许多浏览器还不支持数组对象的新group方法,但很快就会提供(TC 39的第3阶段),并且已经在polyfill core-js中提供。
这将允许您像这样直接执行:

employees.group(employee => employee.company);

或者甚至:

employees.group(({company}) => company);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/group

oogrdqng

oogrdqng2#

let employees = [
  {name: 'Alina', company: 'Google', id : 1},
  {name: 'Vika', company: 'Coca Cola', id : 2},
  {name: 'Alex', company: 'Jonson & Jonson', id : 3},
  {name: 'Vlad', company: 'Google', id : 4},
  {name: 'Fibi', company: 'Coca Cola', id : 5},
  {name: 'Joey', company: 'Google', id : 6},
]
function groupBy(arr, property) {
    return arr.reduce(function (memo, x) {
        if (!memo[x[property]]) { memo[x[property]] = []; }
        memo[x[property]].push(x);
        return memo;
    }, {});
};
console.log(groupBy(employees,'company'));
ua4mk5z4

ua4mk5z43#

最简单的方法是:

let employees = [
  {name: 'Alina' company: 'Google', id : 1},
  {name: 'Vika' company: 'Coca Cola', id : 2},
  {name: 'Alex' company: 'Jonson & Jonson', id : 3},
  {name: 'Vlad' company: 'Google', id : 4},
  {name: 'Fibi' company: 'Coca Cola', id : 5},
  {name: 'Joey' company: 'Google', id : 6},
]

const grouped = groupBy(employees, employee => employee.company);
nmpmafwu

nmpmafwu4#

伙计,这似乎是不尊重潜在的响应者,如果你甚至不检查,如果你的结构是免费的错误,在问一个问题.
因此,存在固定变量:

let companies = ['Google', 'Coca Cola', 'Jonson & Jonson'];
let employees = [
  {name: 'Alina', company: 'Google', id : 1},
  {name: 'Vika', company: 'Coca Cola', id : 2},
  {name: 'Alex', company: 'Jonson & Jonson', id : 3},
  {name: 'Vlad', company: 'Google', id : 4},
  {name: 'Fibi', company: 'Coca Cola', id : 5},
  {name: 'Joey', company: 'Google', id : 6}]

请注意,companies是冗余的,因为所有需要的信息都在employees中。
您要查找的结构可能是Map。您只需执行以下操作:

let map = new Map()
employees.forEach((currentValue) => {
    map.has(currentValue.company) 
    ? map.get(currentValue.company).push({name: currentValue.name, id: currentValue.id})
    : map.set(currentValue.company, [{name: currentValue.name, id: currentValue.id}])
});

要获得此结果(employee对象中不再需要字段company):

{
  Coca Cola: [{
  id: 2,
  name: "Vika"
}, {
  id: 5,
  name: "Fibi"
}],
  Google: [{
  id: 1,
  name: "Alina"
}, {
  id: 4,
  name: "Vlad"
}, {
  id: 6,
  name: "Joey"
}],
  Jonson & Jonson: [{
  id: 3,
  name: "Alex"
}]
}
rqqzpn5f

rqqzpn5f5#

因为你走的是简单的路线,所以会稍微长一点。

let companies = ['Google', 'Coca Cola,' 'Jonson & Jonson',];

let employees = [
  {name: 'Alina' company: 'Google', id : 1},
  {name: 'Vika' company: 'Coca Cola', id : 2},
  {name: 'Alex' company: 'Jonson & Jonson', id : 3},
  {name: 'Vlad' company: 'Google', id : 4},
  {name: 'Fibi' company: 'Coca Cola', id : 5},
  {name: 'Joey' company: 'Google', id : 6},
]

//Let's create an intermediate object 
let interm = {};
/*This would create an object like 
 {
 Key:[],
 Key2:[],
 ...
 }

*/
companies.forEach((each)=>{
    interm[`${each}`] = [];
})

/*filling that interm a
Object with values like

{
'Google':[
{name: 'Alina' company: 'Google', id : 1}, 
{name: 'Vlad' company: 'Google', id : 4},
{name: 'Joey' company: 'Google', id : 6}
],
 Coca Cola:[
{name: 'Vika' company: 'Coca Cola', id : 2},
  {name: 'Fibi' company: 'Coca Cola', id : 5},
],
"Jonson & Jonson":[
 {name: 'Alex' company: 'Jonson & Jonson', id : 3},
]    
}

*/

employee.forEach((each)=>{
    if(companies.indexOf(each.company) != -1)
    interm[`${each.company}`].push(each)

})

//Now our intermediate data is ready 
//We need to convert to our   desirable format
let finalArray = []
Object.keys(interm).forEach((each)=>{
    finalArray.push({each:interm[`${each}`]})
})

console.log(finalArray)

相关问题