我有一个包含员工详细信息和工资信息的数组。我想将员工工资详细信息与员工 ID 分组。
这是输入数组:
let employeeDetails = [{
"employeeDetail": {
"empID": "XXYYZZ11",
"firstname": "abc",
"joinedAt": "13/04/2014",
"address": "VVGGHHNN"
},
"employeeSalaryDetails": [{
"month": "Jan",
"salaryAmount": "35000",
"Bank": "XXXXX",
"PfAccnum": "XXAAQQWWWWQ"
}]
}, {
"employeeDetails": [{
"empID": "XXYYZZ11",
"firstname": "abc",
"joinedAt": "13/04/2014",
"address": "VVGGHHNN"
}],
"employeeSalaryDetails": [{
"month": "Feb",
"salaryAmount": "35000",
"Bank": "XXXXX",
"PfAccnum": "XXAAQQWWWWQ"
}]
}]
这是我想要实现的输出:
let employeeDetails = [{
"employeeDetails": [{
"empID": "XXYYZZ11",
"firstname": "abc",
"joinedAt": "13/04/2014",
"address": "VVGGHHNN"
}],
"employeeSalaryDetails": [{
"month": "Jan",
"salaryAmount": "35000",
"Bank": "XXXXX",
"PfAccnum": "XXAAQQWWWWQ"
}, {
"month": "Feb",
"salaryAmount": "35000",
"Bank": "XXXXX",
"PfAccnum": "XXAAQQWWWWQ"
}]
}]
这是我尝试过的,但没有得到我想要的结果。
function employeeGroup(empArray, key) {
return empArray.reduce((obj, item) => {
(obj[item[key]] = obj[item[key]] || []).push(item)
return obj
}, {})
}
employeeGroup(employeeDetails, "empId")
我是 Javascript 新手。谁可以帮我这个事 ?
慕田峪9158850
POPMUISE
慕工程0101907
慕的地8271018
相关分类