Javascript 两个集合组成一个集合,用更新的字段替换重复项

Javascript 两个集合组成一个集合,用更新的字段替换重复项,javascript,arrays,json,lodash,Javascript,Arrays,Json,Lodash,我有两个独特的系列,看起来像这样: 收集一: [ { id: 123, Name: Ben, Type: Car }, { id: 124, Name: Morgan, Type: Van }, { id: 125, Name: Josh, Type: Bus } ] 第二集: [ { id: 123, Name: Ben, Type: House }, { id: 124, Name: Morgan, Type:

我有两个独特的系列,看起来像这样:

收集一:

[
 {
  id: 123,
  Name: Ben,
  Type: Car 
 },
 {
  id: 124,
  Name: Morgan,
  Type: Van
 },
 {
  id: 125,
  Name: Josh,
  Type: Bus
 }
]
第二集:

[
 {
  id: 123,
  Name: Ben,
  Type: House
 },
 {
  id: 124,
  Name: Morgan,
  Type: Flat
 },
 {
  id: 126,
  Name: Jack,
  Type: Landrover
 }
]
我已使用lodash_uqby确保两个集合中没有重复项

但是,我想将两者合并在一起创建一个集合,但将具有匹配ID的集合替换为类型为“Car”| |==“Van”

因此,我从上述收集中得到的结果是:

结果:

[
 {
  id: 123,
  Name: Ben,
  Type: Car 
 },
 {
  id: 124,
  Name: Morgan,
  Type: Van
 },
 {
  id: 125,
  Name: Josh,
  Type: Bus
 },
 {
  id: 126,
  Name: Jack,
  Type: Landrover
 }
]
无论如何,我可以用lodash来做这个?还是其他方式

提前感谢:)

使用lodash:

function unite(arr1, arr2) {
  return _(arr1)
    .concat(arr2) // concat the arrays
    .groupBy('id') // create a group by the id
    .map(function(group) { // in each group
      return _.find(group, function(item) { // find if one contains the request Type, and if not return the 1st in the group
        return item.Type === 'Car' || item.Type === 'Van'; 
      }) || _.head(group);
    })
    .values() // exract the values
    .value();
}
函数联合(arr1、arr2){
返回(arr1)
.concat(arr2)//concat数组
.groupBy('id')//根据id创建一个组
.map(功能(组){//在每个组中
返回u.find(组,函数(项){//find,如果其中一个包含请求类型,则返回该组中的第一个
返回项目。类型=='Car'| |项目。类型=='Van';
})| | |组长(组);
})
.values()//提取值
.value();
}
变量arr1=[{
id:'123',
名字:“本”,
类型:“汽车”
}, {
id:'124',
姓名:'摩根',
类型:“扁平”
}, {
id:'125',
姓名:“Josh”,
类型:“公共汽车”
}];
var arr2=[{
id:'123',
名字:“本”,
类型:“房子”
}, {
id:'124',
姓名:'摩根',
类型:“货车”
}, {
id:'126',
名字:“杰克”,
类型:“路虎”
}];
var结果=单位(arr1,arr2);
控制台日志(结果)
使用lodash:

function unite(arr1, arr2) {
  return _(arr1)
    .concat(arr2) // concat the arrays
    .groupBy('id') // create a group by the id
    .map(function(group) { // in each group
      return _.find(group, function(item) { // find if one contains the request Type, and if not return the 1st in the group
        return item.Type === 'Car' || item.Type === 'Van'; 
      }) || _.head(group);
    })
    .values() // exract the values
    .value();
}
函数联合(arr1、arr2){
返回(arr1)
.concat(arr2)//concat数组
.groupBy('id')//根据id创建一个组
.map(功能(组){//在每个组中
返回u.find(组,函数(项){//find,如果其中一个包含请求类型,则返回该组中的第一个
返回项目。类型=='Car'| |项目。类型=='Van';
})| | |组长(组);
})
.values()//提取值
.value();
}
变量arr1=[{
id:'123',
名字:“本”,
类型:“汽车”
}, {
id:'124',
姓名:'摩根',
类型:“扁平”
}, {
id:'125',
姓名:“Josh”,
类型:“公共汽车”
}];
var arr2=[{
id:'123',
名字:“本”,
类型:“房子”
}, {
id:'124',
姓名:'摩根',
类型:“货车”
}, {
id:'126',
名字:“杰克”,
类型:“路虎”
}];
var结果=单位(arr1,arr2);
控制台日志(结果)

这是一个纯javascript方案,它不会改变给定的数组

var coll1=[{id:123,名称:'Ben',类型:'Car'},{id:124,名称:'Morgan',类型:'Van'},{id:125,名称:'Josh',类型:'Bus'}],
coll2=[{id:123,名称:“本”,类型:“豪斯”},{id:124,名称:“摩根”,类型:“公寓”},{id:126,名称:“杰克”,类型:“路虎”},
hash=Object.create(null),
合并=coll1.map(函数(o){
散列[o.id]={};
Object.keys(o).forEach(函数(k){
散列[o.id][k]=o[k];
});
返回散列[o.id];
});
coll2.forEach(函数(o){
if(散列[o.id]){
如果(o.Type=='Van'| | o.Type==='Car'){
hash[o.id].Type=o.Type;
}
}否则{
散列[o.id]={};
Object.keys(o).forEach(函数(k){
散列[o.id][k]=o[k];
});
push(hash[o.id]);
}
});
console.log(合并)

.as控制台包装{max height:100%!important;top:0;}
这是一个纯javascript方案,它不会改变给定的数组

var coll1=[{id:123,名称:'Ben',类型:'Car'},{id:124,名称:'Morgan',类型:'Van'},{id:125,名称:'Josh',类型:'Bus'}],
coll2=[{id:123,名称:“本”,类型:“豪斯”},{id:124,名称:“摩根”,类型:“公寓”},{id:126,名称:“杰克”,类型:“路虎”},
hash=Object.create(null),
合并=coll1.map(函数(o){
散列[o.id]={};
Object.keys(o).forEach(函数(k){
散列[o.id][k]=o[k];
});
返回散列[o.id];
});
coll2.forEach(函数(o){
if(散列[o.id]){
如果(o.Type=='Van'| | o.Type==='Car'){
hash[o.id].Type=o.Type;
}
}否则{
散列[o.id]={};
Object.keys(o).forEach(函数(k){
散列[o.id][k]=o[k];
});
push(hash[o.id]);
}
});
console.log(合并)

.as console wrapper{max height:100%!important;top:0;}
您可以对关联列表进行排序,然后根据邻域比较进行筛选:

var list1=[{id:123,名称:“本”,类型:“车”},{id:124,名称:“摩根”,类型:“凡”},{id:125,名称:“乔希”,类型:“公共汽车”},
清单2=[{id:123,名字:“本”,类型:“豪斯”},{id:124,名字:“摩根”,类型:“平坦”},{id:126,名字:“杰克”,类型:“路虎”};
var finalList=list2.concat(list1).sort((a,b)=>a.id-b.id).filter((x,i,arr)=>{
如果(iconsole.log(finalList)您可以对具体列表进行排序,然后根据邻域比较进行筛选:

var list1=[{id:123,名称:“本”,类型:“车”},{id:124,名称:“摩根”,类型:“凡”},{id:125,名称:“乔希”,类型:“公共汽车”},
清单2=[{id:123,名字:“本”,类型:“豪斯”},{id:124,名字:“摩根”,类型:“平坦”},{id:126,名字:“杰克”,类型:“路虎”};
var finalList=list2.concat(list1).sort((a,b)=>a.id-b.id).filter((x,i,arr)=>{
如果(i,然后,然后