在 JavaScript 中,如何求出两个数组的交集和差集

作者: TAIS3 分类: JavaScript 发布时间: 2022-04-04 15:34

Underscore.js

intersection

_.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 otherarrays.

_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
=> [1, 3, 4]

 

但是这个只能用于原生类型,如果想要用于对象数组的话,丢个链接在这里

stackoverflow.com/quest

发表评论