Lodash对象数组的联合 [英] Lodash union of arrays of objects
本文介绍了Lodash对象数组的联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 _。union
函数来创建两个对象数组的并集。 Union只使用基元数组,因为它使用===检查两个值是否相等。
I'd like to use the _.union
function to create a union of two arrays of objects. Union works with arrays of primitives only as it uses === to examine if two values are equal.
我想使用关键属性比较对象:对象与相同的关键财产将被视为平等。是否有一个很好的功能方式来实现理想情况下使用lodash?
I'd like to compare objects using a key property: objects with the same key property would be regarded equal. Is there a nice functional way to achieve that ideally using lodash?
推荐答案
一种非纯粹的lodash方式来做到这一点,但使用数组.concat函数你可以很简单地沿 uniq()
:
A non pure lodash way to do this but using the array.concat function you are able to do this pretty simply along uniq()
:
var objUnion = function(array1, array2, matcher) {
var concated = array1.concat(array2)
return _.uniq(concated, false, matcher);
}
var union = _.uniq(_.flatten([array1, array2]), matcherFn);
这篇关于Lodash对象数组的联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文