Javascript 聚合两个对象数组
假设我有两个阵列:Javascript 聚合两个对象数组,javascript,arrays,aggregate,Javascript,Arrays,Aggregate,假设我有两个阵列: 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}, {n
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}
];
常量初始值:数组=[
{姓名:'a',时报:40,其他:50},
{姓名:'b',时报:10,其他:15},
{姓名:'c',时报:15,其他:12}
];
常量:数组=[
{姓名:'a',时报:45,其他:30},
{name:'c',times:10,other:10},
{姓名:'d',时报:23,其他: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',时报:85,其他:80},
{姓名:'b',时报:10,其他:15},
{姓名:'c',时报:25,其他:22},
{姓名:'d',时报:23,其他:10}
];
我想知道在两个阵列之间聚合数据的最佳方法是什么。下面是我过去如何处理这个问题的
final
数组,该数组将包含最终结果concat
方法合并数组名称
属性排序,这意味着所有的a都是有序的,然后是b,等等forEach
方法用于迭代连接的排序数组。如果当前元素与final
数组的最后一个元素具有相同的name
属性,则将el
的数值属性添加到final
数组的最后一个元素。否则,将el
添加到最终数组的末尾const initial=[
{姓名:'a',时报:40,其他:50},
{姓名:'b',时报:10,其他:15},
{姓名:'c',时报:15,其他:12}
];
常数=[
{姓名:'a',时报:45,其他:30},
{name:'c',times:10,other:10},
{姓名:'d',时报:23,其他:10}
];
设final=[];
initial.concat(tobat).sort((a,b)=>a.name>b.name).forEach(el=>{
如果(final.length>0&&el.name==final[final.length-1].name){
final[final.length-1]。次数+=el.times;
final[final.length-1]。其他+=el.other;
}否则{
最终推(el);
}
})
控制台日志(最终)代码>您可以在的帮助下完成
首先使用concat
合并两个数组
现在使用reduce
在concated数组上检查对象属性是否已经存在于output
中,然后在现有属性中添加times
和other
,如果不存在,则在现有属性中添加新属性李>
const initial=[{name:'a',times:40,other:50},{name:'b',times:10,other:15},{name:'c',times:15,other:12}];
常量=[{name:'a',times:45,other:30},{name:'c',times:10,other:10},{name:'d',times:23,other:10}];
让温度=初始浓度(TOBAT)
让op=温度降低((输出,电流)=>{
if(输出[current.name]){
输出[current.name].times+=current.times
输出[current.name].other+=current.other
}否则{
输出[当前.名称]=当前;
}
返回输出;
},{})
console.log(Object.values(op))
试试下面的代码
const initial=[
{姓名:'a',时报:40,其他:50},
{姓名:'b',时报:10,其他:15},
{姓名:'c',时报:15,其他:12}
];
常数=[
{姓名:'a',时报:45,其他:30},
{name:'c',times:10,other:10},
{姓名:'d',时报:23,其他:10}
];
//控制台日志(首字母);
函数arrayUnique(数组){
var a=array.concat();
对于(var i=0;i您可以减少给定的数据并查找相同的名称
,然后更新,否则添加一个新对象
需要
- 一个新数组,其中包含来自
initial
和的项
- 减少项目数量
- 在累加器
r
中查找具有相同名称的项目
- 检查是否未找到该项,然后返回一个包含收集的ITM和实际对象副本的新数组
- 否则,使用一些值增加所需的属性
const
initial=[{name:'a',times:40,other:50},{name:'b',times:10,other:15},{name:'c',times:15,other:12}],
TOBER=[{name:'a',times:45,other:30},{name:'c',times:10,other:10},{name:'d',times:23,other:10}],
合并=[…初始值,…合并]。减少((r,o)=>{
var temp=r.find(p=>o.name==p.name);
如果(!temp)返回[…r,{…o}];
温度时间+=o时间;
其他温度+=其他温度;
返回r;
}, []);
console.log(merged);
这里有一种方法,使用并覆盖要合并的新数组。如果要合并的新对象已经存在(即某个对象的名称
属性匹配),我们将增加其余匹配属性并添加不存在的属性,否则我们将推送整个新对象:
const initial=[
{姓名:'a',时报:40,其他:50},
{姓名:'b',时报:10,其他:15},
{姓名:'c',时报:15,其他:12}
];
常数=[
{姓名:'a',时报:45,其他:30,其他:76},
{name:'c',times:10,other:10},
{姓名:'d',时报:23,其他:10}
];
让newArray=tobay.reduce((res,curr)=>
{
让found=res.findIndex(x=>x.name==curr.name);
如果(发现>=0)
{
res[found]=Object.keys(curr.reduce)(r,c)=>
{
r[[c]]=(r[[c]]和&c!=='name')?r[[c]]+curr[[c]]:curr[[c]];
返回r;
},res[found]);
}
其他的
{
推力(电流);
}
返回res;
},首字母);
log(newArray);
我将通过合并两个数组来实现这一点,然后对合并后的数组运行reduce
在reduce内部,它首先检查是否存在同名的条目,如果不存在,则将该条目推送到结果数组中。如果确实找到现有条目,则创建任何不存在的属性,并添加任何num的值