唯一的对象数组,并使用lodash或下划线合并合并重复项 [英] Unique array of objects and merge duplications using lodash or underscore
问题描述
我有一个可以包含重复项的对象数组,但是我想通过一个属性来唯一化该数组,然后我想有条件地合并重复项的属性。例如,给下面的数组
var array = [
{
name:'object1',
属性A:true,
属性B:false,
属性C:false
},
{
名称: object2,
属性A: false,
属性B:true,
属性C:false
},
{
name:'object1',
propertyA:false,
propertyB:true,
propertyC:false
}
]
知道每个对象都有名称,propertyA,propertyB和propertyC,我想按名称property来唯一化,并保留propertyA,propertyB和propertyC的真实值。这样上面的数组就变成了
varupdatedArray = [
{
name:'object1',
属性A:true,
属性B:true,
属性C:false
},
{
name:'object2',
propertyA:false ,
属性B:true,
属性C:false
}
]
使用lodash /下划线解决此问题的最佳方法是什么?
您可以使用< a href = https://lodash.com/docs/4.17.4#uniqWith rel = nofollow noreferrer> _。uniqWith
,但是会要求您先更改其中一个参数,然后再返回它们是否相等的布尔值,所以我不建议这样做。
更好的选择是使用< a href = https://lodash.com/docs/4.17.4#groupBy rel = nofollow noreferrer> _。groupBy
后跟< a href = https://lodash.com/docs/4.17.4#map rel = nofollow noreferrer> _。map
,以便对唯一名称进行分组,然后自定义组合方式。在您的情况下,可以使用 _。assignWith组合相同名称的对象
,然后给出逻辑或 |||
操作作为组合函数。
使用ES6,它几乎变成了一种形式:
var array = [{{name}: object1, propertyA:true, propertyB:false, propertyC:false},{ name: object2, propertyA:false, propertyB: true, propertyC:false},{ name: object1, propertyA:false, propertyB:true, propertyC:false}]]; var UpdatedArray = _(array).groupBy('name').map(objs => _.assignWith({},... objs,(val1,val2)=> val1 || val2)).value( ); console.log(updatedArray);
.as-console-包装器{max-height:100%!important;最高:0; }
< script src = https:// cdnjs .cloudflare.com / ajax / libs / lodash.js / 4.17.4 / lodash.min.js>< / script>
I have an array of objects that can contain duplicates, but I'd like uniquify the array by a property, and then I want to merge properties of the duplicates conditionally. For example, give the array below
var array = [
{
name: 'object1',
propertyA: true,
propertyB: false,
propertyC: false
},
{
name: 'object2',
propertyA: false,
propertyB: true,
propertyC: false
},
{
name: 'object1',
propertyA: false,
propertyB: true,
propertyC: false
}
]
Knowing each object has name, propertyA, propertyB, and propertyC, I'd like to uniquify by the name property, and keep the true values for propertyA, propertyB, and propertyC. So the above array becomes,
var updatedArray = [
{
name: 'object1',
propertyA: true,
propertyB: true,
propertyC: false
},
{
name: 'object2',
propertyA: false,
propertyB: true,
propertyC: false
}
]
What is the best way to go about this using lodash/underscore?
You could do it using _.uniqWith
, but it would require you to mutate one of the parameters before returning the boolean value of whether they are equal, so I don't recommend it.
A better option is to use _.groupBy
followed by _.map
in order to group the unique names, and then customize how you combine them. In your case, you can combine the same-named objects using _.assignWith
and then give a logical or ||
operation as the combining function.
With ES6, it becomes nearly a one-liner:
var array = [{"name":"object1","propertyA":true,"propertyB":false,"propertyC":false},{"name":"object2","propertyA":false,"propertyB":true,"propertyC":false},{"name":"object1","propertyA":false,"propertyB":true,"propertyC":false}];
var updatedArray =
_(array)
.groupBy('name')
.map(objs => _.assignWith({}, ...objs, (val1, val2) => val1 || val2))
.value();
console.log(updatedArray);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
这篇关于唯一的对象数组,并使用lodash或下划线合并合并重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!