假设我有这两个数组:
const initial: Array<GivenObj> = [
{name: 'a', times: 40, other: 50},
{name: 'b', times: 10, other: 15},
{name: 'c', times: 15, other: 12}
];
const toBeMerged: Array<GivenObj> = [
{name: 'a', times: 45, other: 30},
{name: 'c', times: 10, other: 10},
{name: 'd', times: 23, other: 10}
];
这两个数组包含不同的值,但具有相似的键。我需要将这些数据聚合到一个数组中,该数组将包含它们的所有值,但每个键只出现一次。
在代码中,应按以下方式聚合上述两个数组:
const aggregated: Array<GivenObj> = [
{name: 'a', times: 85, other: 80},
{name: 'b', times: 10, other: 15},
{name: 'c', times: 25, other: 22},
{name: 'd', times: 23, other: 10}
];
我在想,如何最好地将两个数组的数据聚合起来。
a
的times
结果应该是85,而c
对于两个值都是错误的。 - Andy