为了账号安全,请及时绑定邮箱和手机立即绑定

javascript 对数组中匹配相同键的所有项目求和

javascript 对数组中匹配相同键的所有项目求和

慕斯709654 2023-06-29 15:43:48
我有一个像这样的 JSON:  [   {      "faculty":{         "_id":"5f9c3e04e5a5423cec34b2e2",         "code":"khmt",         "facultyName":"Computer Science"      },      "classSize":91,      "warnedLength":61   },   {      "faculty":{         "_id":"5f9c53c8a0db8f4240ec6f71",         "code":"dtvt",         "facultyName":"Electronics and Telecommunication"      },      "classSize":89,      "warnedLength":44   },   {      "faculty":{         "_id":"5f9c53c8a0db8f4240ec6f71",         "code":"dtvt",         "facultyName":"Electronics and Telecommunication"      },      "classSize":59,      "warnedLength":20   },   {      "faculty":{         "_id":"5f9c53c8a0db8f4240ec6f71",         "code":"hkvt",         "facultyName":"Space Airline"      },      "classSize":10,      "warnedLength":5   },   {      "faculty":{         "_id":"5f9c53c8a0db8f4240ec6f71",         "code":"hkvt",         "facultyName":"Space Airline"      },      "classSize":20,      "warnedLength":10   },   {      "faculty":{         "_id":"5f9c53c8a0db8f4240ec6f71",         "code":"hkvt",         "facultyName":"Space Airline"      },      "classSize":30,      "warnedLength":15   }]我想要这样的输出:[   {      "faculty":{         "_id":"5f9c3e04e5a5423cec34b2e2",         "code":"khmt",         "facultyName":"Computer Science"      },      "totalClassSize":91,      "totalWarnedLength":61   },   {      "faculty":{         "_id":"5f9c53c8a0db8f4240ec6f71",         "code":"dtvt",         "facultyName":"Electronics and Telecommunication"      },      "totalClassSize":148, // total classSize of all class has facultyName is "Electronics and Telecommunication"      "totalWarnedLength":64 // total warnedLength of all class has facultyName is "Electronics and Telecommunication"   },输出是一个数组,其总和classSize具有相同的属性属性totalClassSize,并且总和warnedLength具有相同的属性属性totalWarnedLength输入是一个数组,输出也仍然是一个数组我是学习代码的初学者,但我仍然被这个问题困扰了一段时间非常感谢你帮助我,祝你有美好的一天
查看完整描述

1 回答

?
狐的传说

TA贡献1804条经验 获得超3个赞

使用Array.prototype.reduce,您可以通过将当前输入数组作为faculty._id对象键进行分组来生成新对象,并基于此,您可以计算相同对象键的totalWarnedLength和。totalClassSize

const input = [{

    "faculty": {

      "_id": "5f9c3e04e5a5423cec34b2e2",

      "code": "khmt",

      "facultyName": "Computer Science"

    },

    "classSize": 91,

    "warnedLength": 61

  },

  {

    "faculty": {

      "_id": "5f9c53c8a0db8f4240ec6f71",

      "code": "dtvt",

      "facultyName": "Electronics and Telecommunication"

    },

    "classSize": 89,

    "warnedLength": 44

  },

  {

    "faculty": {

      "_id": "5f9c53c8a0db8f4240ec6f71",

      "code": "dtvt",

      "facultyName": "Electronics and Telecommunication"

    },

    "classSize": 59,

    "warnedLength": 20

  },

  {

    "faculty": {

      "_id": "5f9c53c8a0db8f4240ec6f71",

      "code": "hkvt",

      "facultyName": "Space Airline"

    },

    "classSize": 10,

    "warnedLength": 5

  },

  {

    "faculty": {

      "_id": "5f9c53c8a0db8f4240ec6f71",

      "code": "hkvt",

      "facultyName": "Space Airline"

    },

    "classSize": 20,

    "warnedLength": 10

  },

  {

    "faculty": {

      "_id": "5f9c53c8a0db8f4240ec6f71",

      "code": "hkvt",

      "facultyName": "Space Airline"

    },

    "classSize": 30,

    "warnedLength": 15

  }

];


const groupBy = input.reduce((acc, cur) => {

  if (acc[cur.faculty['_id']]) {

    acc[cur.faculty['_id']].totalClassSize += cur.classSize;

    acc[cur.faculty['_id']].totalWarnedLength += cur.warnedLength;

  } else {

    acc[cur.faculty['_id']] = {

      faculty: cur.faculty,

      totalClassSize: cur.classSize,

      totalWarnedLength: cur.warnedLength

    };

  }

  return acc;

}, {});

const output = Object.values(groupBy);

console.log(output);





查看完整回答
反对 回复 2023-06-29
  • 1 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信