如何在Javascript中使用其对象键对数组进行分组

我有一个包含员工详细信息和工资信息的数组。我想将员工工资详细信息与员工 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 新手。谁可以帮我这个事 ?


繁星coding
浏览 105回答 4
4回答

慕田峪9158850

使用解构将第一个数组元素提取为 item,并使用 employeeSalaryDetails 属性。带有 empID 的对象包含在数组中。您没有推送 meployeeSalaryDetails 对象。此外,您的 empId 应该是 empID。function employeeGroup(empArray, key) {&nbsp; return Object.values(empArray.reduce(&nbsp; (obj,{employeeDetails: [item], employeeSalaryDetails}) => {&nbsp; &nbsp; &nbsp; (obj[item[key]] = obj[item[key]] ||&nbsp; &nbsp; &nbsp; &nbsp;{ employeeDetails: item, employeeSalaryDetails:[] })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.employeeSalaryDetails.push(...employeeSalaryDetails)&nbsp; &nbsp; &nbsp; return obj&nbsp; }, {}))}console.log(&nbsp; employeeGroup(employeeDetails, "empID"))<script>&nbsp; let employeeDetails = [{&nbsp; &nbsp; "employeeDetails": [{&nbsp; &nbsp; &nbsp; "empID": "XXYYZZ11",&nbsp; &nbsp; &nbsp; "firstname": "abc",&nbsp; &nbsp; &nbsp; "joinedAt": "13/04/2014",&nbsp; &nbsp; &nbsp; "address": "VVGGHHNN"&nbsp; &nbsp; }],&nbsp; &nbsp; "employeeSalaryDetails": [{&nbsp; &nbsp; &nbsp; "month": "Jan",&nbsp; &nbsp; &nbsp; "salaryAmount": "35000",&nbsp; &nbsp; &nbsp; "Bank": "XXXXX",&nbsp; &nbsp; &nbsp; "PfAccnum": "XXAAQQWWWWQ"&nbsp; &nbsp; }]&nbsp; }, {&nbsp; &nbsp; "employeeDetails": [{&nbsp; &nbsp; &nbsp; "empID": "XXYYZZ11",&nbsp; &nbsp; &nbsp; "firstname": "abc",&nbsp; &nbsp; &nbsp; "joinedAt": "13/04/2014",&nbsp; &nbsp; &nbsp; "address": "VVGGHHNN"&nbsp; &nbsp; }],&nbsp; &nbsp; "employeeSalaryDetails": [{&nbsp; &nbsp; &nbsp; "month": "Feb",&nbsp; &nbsp; &nbsp; "salaryAmount": "35000",&nbsp; &nbsp; &nbsp; "Bank": "XXXXX",&nbsp; &nbsp; &nbsp; "PfAccnum": "XXAAQQWWWWQ"&nbsp; &nbsp; }]&nbsp; }]</script>

POPMUISE

您可以使用reduce和 过滤掉您可以使用 和 的重复员工filter记录some。这是一个工作示例。var employeeDetails = [{ "employeeDetails": [ { "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" } ] }];var result = [employeeDetails.reduce((acc, {employeeDetails, employeeSalaryDetails})=>{&nbsp; &nbsp; acc['employeeDetails'] = acc['employeeDetails'] || [];&nbsp; &nbsp; employeeDetails = employeeDetails.filter(k=>!acc['employeeDetails'].some(l=>l.empID==k.empID));&nbsp; &nbsp; acc['employeeDetails'] = [...acc['employeeDetails'], ...employeeDetails];&nbsp; &nbsp; acc['employeeSalaryDetails'] = [...(acc['employeeSalaryDetails'] || []), ...employeeSalaryDetails];&nbsp; &nbsp; return acc;},{})];console.log(result);

慕工程0101907

/**&nbsp;*&nbsp;&nbsp;* @param {item[]} arr&nbsp;&nbsp;* @param {(item)=>item[<GroupField>]} g&nbsp;&nbsp;* @returns&nbsp;&nbsp;*/function groupBy ( arr, g ) {&nbsp; &nbsp; let len = arr.length&nbsp; &nbsp; for ( let i = 0; i < len; i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; const a = arr.pop();&nbsp; &nbsp; &nbsp; &nbsp; const b = g( a );&nbsp; &nbsp; &nbsp; &nbsp; const c = arr.find( e => e.d == b )&nbsp; &nbsp; &nbsp; &nbsp; if ( c ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.e.push( a )&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr.unshift( ( {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d: g( a ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e: [ a ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } ) )&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return arr.map( e => ( { [ e.d ]: e.e } ) ).reduce( ( p, c ) => ( { ...p, ...c } ) );}const result = groupBy( [&nbsp; &nbsp; { name: 'a', group: 1 },&nbsp; &nbsp; { name: "b", group: 1 },&nbsp; &nbsp; { name: "c", group: 10 }], ( item ) => item.group );console.log( result );

慕的地8271018

假设您的数组将只包含一个对象,则以下代码将起作用。&nbsp; &nbsp; let employeeDetails = [{ "employeeDetails": [{ "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" }] }]&nbsp; &nbsp; function getEmployeeDetails(id){&nbsp; &nbsp; &nbsp; &nbsp; let employeeDetail = {employeeDetails:[],employeeSalaryDetails:[]};&nbsp; &nbsp; &nbsp; &nbsp; employeeDetails.forEach(function(eachDetail){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let ed = eachDetail.employeeDetails[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let es = eachDetail.employeeSalaryDetails[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(ed.empID == id){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; employeeDetail.employeeDetails = ed;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; employeeDetail.employeeSalaryDetails.push(es);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; return [employeeDetail];&nbsp; &nbsp; }&nbsp; &nbsp; console.log(getEmployeeDetails("XXYYZZ11"));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript