并集,交集,差集 - union, intersection, difference

0
视频
0
完成
0%
进度
0
分钟
0
完成
0%
进度

把几个数组合并到一块儿,去掉重复的项目,得到的就是两个数组的并集。可以使用 union 这个方法。现在,我们这里有两个数组,fruits,还有 foods ... 下面用 union 方法,得到这两个数组的并集 ..

union ... fruits 逗号 ... foods .. 如果有其它的数组,可以继续用逗号分隔一下,再添加进来 ..

_.union(fruits, foods)

得到的结果就是,fruits 和 foods 这两个数组的并集 ... 注意这两个数组里面,都有 apple 这个项目 ... 同并集以后,只会保留一个 apple ...

intersection

如果想要得到的是数组之间的交集 .. 要用的是 intersection 方法 ..

_.intersection(fruits, foods)

返回的结果是 apple ... 因为在这两个数组里面,共有的项目只有 apple ...

difference

要得到一个数组跟其它数组的差集,也就是在这个数组里面有的,在其它数组里没有的项目 ... 可以使用 difference ...

_.difference(fruits, foods)

返回的结果是 orange, banan, 还有 pear 这几个项目 ... 去掉了 fruits 里面的 apple 这个项目 ... 因为在 foods 里面,也包含这个 apple 项目。

并集,交集,差集 - union, intersection, difference《 Underscore 基础 》

统计

14696
分钟
0
你学会了
0%
完成

社会化网络

关于

微信订阅号

扫描微信二维码关注宁皓网,每天进步一点