Underscore.JS - 交集操作方法



语法

_.intersection(*arrays)

交集操作方法返回传入数组的交集,即每个数组中公共值组成的数组。

示例

var _ = require('underscore');

var list1 = [1, 2, 3, 4, 5, 6]
var list2 = [1, 2, 3, 7]
var list3 = [1, 2, 4, 5, 6, 7, 8]
//Example 1: intersection of list1 and list2
result = _.intersection(list1, list2);
console.log(result)

//Example 2: intersection of list1, list2, list3
result = _.intersection(list1, list2, list3);
console.log(result)

tester.js 中保存以上程序。运行以下命令以执行该程序。

命令

\>node tester.js

输出

[ 1, 2, 3 ]
[ 1, 2 ]
underscorejs_processing_array.htm
广告
© . All rights reserved.