如何使用唯一键合并对象数组?
How can i merge an array of objects with with a unique key?
我有以下数组 objects.i 想要将这些对象与月份合并为唯一 ID。
[
{ month: 7, openSlot: 9, confirmed: 0, requested: 0, total: 0 },
{ month: 5, openSlot: 0, confirmed: 6, requested: 0, total: 0 },
{ month: 7, openSlot: 0, confirmed: 0, requested: 0, total: 17 }
]
上面的数组应该这样合并
{ month: 7, openSlot: 9, confirmed: 0, requested: 0, total: 17 },
{ month: 5, openSlot: 0, confirmed: 6, requested: 0, total: 0 }
您可以将 month
与对象分开,并通过它们的键添加所有其他值。
const
data = [{ month: 7, openSlot: 9, confirmed: 0, requested: 0, total: 0 }, { month: 5, openSlot: 0, confirmed: 6, requested: 0, total: 0 }, { month: 7, openSlot: 0, confirmed: 0, requested: 0, total: 17 }],
result = Object.values(data.reduce((r, { month, ...o }) => {
r[month] ??= { month };
Object
.entries(o)
.forEach(([k, v]) => r[month][k] = (r[month][k] || 0) + v);
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以使用 forEach
并添加每个键的值来完成此操作。注意:如果您对更优化的代码感兴趣,请使用
的解决方案
const data = [
{ month: 7, openSlot: 9, confirmed: 0, requested: 0, total: 0 },
{ month: 5, openSlot: 0, confirmed: 6, requested: 0, total: 0 },
{ month: 7, openSlot: 0, confirmed: 0, requested: 0, total: 17 }
]
function merge(data) {
let tempObj = {};
data.forEach(item => {
if (tempObj[item['month']]) {
let temp = {}
Object.entries(item).forEach(([key, value]) => {
temp[key] = key === 'month' ? value : value + tempObj[item['month']][key];
})
tempObj[item['month']] = temp
} else {
tempObj[item['month']] = item
}
});
return tempObj;
}
console.log(Object.values(merge(data)));
我有以下数组 objects.i 想要将这些对象与月份合并为唯一 ID。
[
{ month: 7, openSlot: 9, confirmed: 0, requested: 0, total: 0 },
{ month: 5, openSlot: 0, confirmed: 6, requested: 0, total: 0 },
{ month: 7, openSlot: 0, confirmed: 0, requested: 0, total: 17 }
]
上面的数组应该这样合并
{ month: 7, openSlot: 9, confirmed: 0, requested: 0, total: 17 },
{ month: 5, openSlot: 0, confirmed: 6, requested: 0, total: 0 }
您可以将 month
与对象分开,并通过它们的键添加所有其他值。
const
data = [{ month: 7, openSlot: 9, confirmed: 0, requested: 0, total: 0 }, { month: 5, openSlot: 0, confirmed: 6, requested: 0, total: 0 }, { month: 7, openSlot: 0, confirmed: 0, requested: 0, total: 17 }],
result = Object.values(data.reduce((r, { month, ...o }) => {
r[month] ??= { month };
Object
.entries(o)
.forEach(([k, v]) => r[month][k] = (r[month][k] || 0) + v);
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以使用 forEach
并添加每个键的值来完成此操作。注意:如果您对更优化的代码感兴趣,请使用
const data = [
{ month: 7, openSlot: 9, confirmed: 0, requested: 0, total: 0 },
{ month: 5, openSlot: 0, confirmed: 6, requested: 0, total: 0 },
{ month: 7, openSlot: 0, confirmed: 0, requested: 0, total: 17 }
]
function merge(data) {
let tempObj = {};
data.forEach(item => {
if (tempObj[item['month']]) {
let temp = {}
Object.entries(item).forEach(([key, value]) => {
temp[key] = key === 'month' ? value : value + tempObj[item['month']][key];
})
tempObj[item['month']] = temp
} else {
tempObj[item['month']] = item
}
});
return tempObj;
}
console.log(Object.values(merge(data)));