在javascript中合并部分重复的数组(lodash)

在javascript中合并部分重复的数组(lodash),javascript,arrays,lodash,Javascript,Arrays,Lodash,我有一个很大的javascript数组,其中有一些人在不同的年份买了一辆车。 简化的数组如下所示: const owners = [{ name: "john", hasCar: true, yearBought: 2002 }, { name: "john", hasCar: true, yearBought: 2005 }, { name: "mary", hasCar: true, yearBought: 2015 }, { name: "john"

我有一个很大的javascript数组,其中有一些人在不同的年份买了一辆车。 简化的数组如下所示:

const owners = [{
  name: "john",
  hasCar: true,
  yearBought: 2002
}, {
  name: "john",
  hasCar: true,
  yearBought: 2005
}, {
  name: "mary",
  hasCar: true,
  yearBought: 2015
}, {
  name: "john",
  hasCar: true,
  yearBought: 2018
}]
const owners = [{
  name: "john",
  hasCar: true,
  yearBought: [2002, 2005, 2018]
}, {
  name: "mary",
  hasCar: true,
  yearBought: 2018
}]
如果一个人拥有不止一辆汽车(如本例中的约翰),那么他购买汽车的年份不同,其购买对象也不同。我想合并属于每个人的对象,最终结果如下:

const owners = [{
  name: "john",
  hasCar: true,
  yearBought: 2002
}, {
  name: "john",
  hasCar: true,
  yearBought: 2005
}, {
  name: "mary",
  hasCar: true,
  yearBought: 2015
}, {
  name: "john",
  hasCar: true,
  yearBought: 2018
}]
const owners = [{
  name: "john",
  hasCar: true,
  yearBought: [2002, 2005, 2018]
}, {
  name: "mary",
  hasCar: true,
  yearBought: 2018
}]
您可以首先使用数组并根据
name
对它们进行分组。累加器是一个对象,每个唯一的
名称都作为键。如果
名称
已存在,请使用将其推入数组。否则,在累加器中创建一个新键并将其设置为当前对象。然后,使用获取数组作为数组的值

const owner=[{name:“john”,hasCar:!0,年购:2002},{name:“john”,hasCar:!0,年购:2005},{name:“mary”,hasCar:!0,年购:2015},{name:“john”,hasCar:!0,年购:2018}];
const merged=所有者。reduce((r,o)=>{
if(r[o.name])
r[o.name].yearbunded=[].concat(r[o.name].yearbunded,o.yearbunded)
其他的
r[o.name]={…o};
返回r;
},{})

console.log(Object.values(merged))
只需使用
reduce

const owners=[{name:“john”,hasCar:true,yearbunded:2002},{name:“john”,hasCar:true,yearbunded:2005},{name:“mary”,hasCar:true,yearbunded:2015},{name:“john”,hasCar:true,yearbunded:2018}];
const newOwners=Object.values(owners.reduce)(acc,curr)=>{
acc[curr.name]=acc[curr.name]?{…acc[curr.name],yearbunded:[].concat(acc[curr.name].yearbunded,curr.yearbunded)}:curr;
返回acc;
}, {}));

console.log(新所有者)我希望这对使用纯lodash函数有所帮助。。它看起来干净易读

var数组=[{
姓名:“约翰”,
哈斯卡:是的,
购买年份:2002年
}, {
姓名:“约翰”,
哈斯卡:是的,
购买年份:2005年
}, {
姓名:“玛丽”,
哈斯卡:是的,
年份:2015年
}, {
姓名:“约翰”,
哈斯卡:是的,
年份:2018年
}]
函数合并名称(arr){
返回Object.values(u.chain(arr).groupBy('name').mapValues((g)=>(u.merge(…g){
yearbunded:uu.map(g,“yearbunded”)
}))).value());
}
log(合并名称(数组))

您可以使用唯一键对项目进行分组(
在您的情况下为name
),然后映射组,并合并每个组中的项目:

const{flow,partiOK:pr,groupBy,map,mergeWith,concat,isUndefined}=_
const mergeDuplicates=(isCollected,key)=>flow(
pr(groupBy,key),//按唯一键分组
pr(map,group=>mergeWith({},…group,
(o,s,k)=>isCollected(k)和isUndefined(o)?concat(o,s):s
))//将每个组合并到一个新对象
)
const owners=[{name:“john”,hasCar:true,yearbunded:2002},{name:“john”,hasCar:true,yearbunded:2005},{name:“mary”,hasCar:!0,yearbunded:2015},{name:“john”,hasCar:true,yearbunded:2018}]
const isCollected=key=>key=='yearbunded'
const result=mergeDuplicates(isCollected,'name')(所有者)
console.log(结果)

我想你不需要用洛达斯。只需检查具有给定名称的元素是否存在,如果存在,请将年份推到
yearsbound
,如果不存在,只需将新元素推到列表上。这会为
mary
创建一个
yearbunded
数组。我相信它对于每种情况都应该是一致的,也就是说,
yearbunded
有一个或多个值。通过这种方式,您可以轻松地管理它