在 JavaScript 中,如何求出两个数组的交集和差集?
13 个回答
用
Underscore.jsintersection
_.intersection(*arrays)
Computes the list of values that are the intersection of all the
arrays
. Each value in the result is present in each of the
arrays
.
_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
=> [1, 2]
difference
_.difference(array, *others)
Similar to
without
, but returns the values from
array
that are not present in the
other
arrays.
_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
=> [1, 3, 4]
但是这个只能用于原生类型,如果想要用于对象数组的话,丢个链接在这里
http:// stackoverflow.com/quest ions/8672383/how-to-use-underscores-intersection-on-objects