【发布时间】:2021-06-29 20:48:12
【问题描述】:
我想合并数组中的对象,以便具有相同 id 的对象(这是一个子对象)可以对 total_price 和 total_quantity 值求和。 这是我的数据数组:
var data = [
{
"_id": {
"month": 5,
"year": 2021
},
"total_price": 145111500,
"total_quantity": 7
},
{
"_id": {
"month": 6,
"year": 2021
},
"total_price": 98386000,
"total_quantity": 5
},
{
"_id": {
"month": 6,
"year": 2021
},
"total_price": 32500000,
"total_quantity": 3
}
]
我想合并具有重复“_id”的对象。这是输出结果:
var merge = [
{
"_id": {
"month": 5,
"year": 2021
},
"total_price": 145111500,
"total_quantity": 7
},
{
"_id": {
"month": 6,
"year": 2021
},
"total_price": 130886000,
"total_quantity": 8
}
]
提前致谢。
【问题讨论】:
标签: javascript arrays object array-merge