[英]Create an array of objects from another array grouping duplicates in Javascript
I need to re-organize an array of objects (from a database) into different groups according to one of the attributes of objects.我需要根据对象的一个属性将一组对象(来自数据库)重新组织到不同的组中。 To make it clearer, let's take the following example:
为了更清楚地说明,让我们看下面的例子:
The ORM (Sequelize) returns the following array object: ORM(Sequelize)返回以下数组 object:
[
{
id: 182,
employeeId: 'a',
skillId: 207,
score: 3,
},
{
id: 512,
employeeId: 'a',
skillId: 212,
score: 4,
},
{
id: 908,
employeeId: 'b',
skillId: 134,
score: 2,
},
{
id: 876,
employeeId: 'c',
skillId: 212,
score: 3,
},
]
I need to re-categorize the object grouping the entries by employeeId.我需要重新分类 object 按 employeeId 对条目进行分组。 Like this:
像这样:
[
{
employeeId: 'a',
skills:
[
{ skillId: 203, score: 3},
{ skillId: 212, score: 4}
]
},
{
employeeId: 'b',
skills:
[
{ skillId: 134, score: 2}
]
},
{
employeeId: 'c',
skills:
[
{ skillId: 212, score: 3}
]
}
]
It didn't look hard at first but I'm hitting a roadblock, I already tried using reduce, forEarch and map but I only got to make an array of employeeIds, can't really get to include the skills array in each object of the array.起初看起来并不难,但我遇到了障碍,我已经尝试使用 reduce、forEarch 和 map,但我只需要制作一个 employeeIds 数组,不能真正在每个 object 中包含技能数组阵列。
Any help would be appreciated.任何帮助,将不胜感激。
const result = [ { id: 182, employeeId: 'a', skillId: 207, score: 3, }, { id: 512, employeeId: 'a', skillId: 212, score: 4, }, { id: 908, employeeId: 'b', skillId: 134, score: 2, }, { id: 876, employeeId: 'c', skillId: 212, score: 3, }, ] const tempObj = result.reduce((acc, curr) => { if (curr.employeeId in acc) { acc[curr.employeeId].push({ skillId: curr.skillId, score: curr.score }) } else { acc[curr.employeeId] = [{ skillId: curr.skillId, score: curr.score }] } return acc }, {}) const reorganized = Object.keys(tempObj).map(key => ({ employeeId: key, skills: tempObj[key] })) console.log(JSON.stringify(reorganized, null, 2))
This approach uses classes to build up the employee object and could be easily extended in the future.这种方法使用类来构建员工 object,并且可以在将来轻松扩展。
const input = [{ id: 182, employeeId: 'a', skillId: 207, score: 3, }, { id: 512, employeeId: 'a', skillId: 212, score: 4, }, { id: 908, employeeId: 'b', skillId: 134, score: 2, }, { id: 876, employeeId: 'c', skillId: 212, score: 3, }, ]; const employeeMap = {}; class Skill { constructor({ skillId, score }) { this.skillId = skillId; this.score = score; } } class Employee { constructor(rawEmployee) { this.employeeId = rawEmployee.employeeId; this.skills = []; } addSkill(rawEmployee) { this.skills.push(new Skill(rawEmployee)); } } for (let i = 0; i < input.length; i++) { const rawEmployee = input[i]; let employee = employeeMap[rawEmployee.employeeId]; if (;employee) { employee = new Employee(rawEmployee). employeeMap[employee;employeeId] = employee. } if (employee) { employee;addSkill(rawEmployee); } } const employeeArray = []. for (let key in employeeMap) { employeeArray;push(employeeMap[key]). } console;log(employeeArray). console.log(JSON;stringify(employeeArray));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.