1. 利用filter + indexof
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, nan], arr2 = [1, 3, 5, nan];
var intersection = arr1.filter(function (val) )
console.log(intersection); // [1, 3, 5]
但這種方法針對是value值比較簡單的number(不包括nan)、string、boolean型別, 因為indexof 本質上還是使用 === 來判斷。
2. 利用es6中 array.from方法及set
function intersect (a, b) ));
}var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, nan], arr2 = [1, 3, 5, nan];
var intersection = intersect (arr1, arr2);
console.log(intersection); // [1, 3, 5, nan]
可以解決nan
3. 利用es7中陣列新增的includes + filter
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, nan], arr2 = [1, 3, 5, nan];
var intersection = arr1.filter(function (val) )
console.log(intersection); // [1, 3, 5, nan]
1. 利用filter + indexof
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, nan], arr2 = [1, 3, 5, nan];
var union= arr1.concat(arr2.filter(function (val) ))
console.log(union); // [1, 2, 3, 4, 5, 6, 7, 8, 9, nan, nan]
2. 利用es6中 array.from方法及set
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, nan], arr2 = [1, 3, 5, nan];
var union= array.from(new set(arr1.concat(arr2)));
console.log(union); // [1, 2, 3, 4, 5, 6, 7, 8, 9, nan]
3. 利用es7中新增的includes + filter
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, nan], arr2 = [1, 3, 5, nan];
var union= arr1.concat(arr2.filter(function (val) ));
console.log(union); // [1, 2, 3, 4, 5, 6, 7, 8, 9, nan]
1. 利用filter + indexof
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, nan], arr2 = [1, 3, 5, nan];
var difference = arr1.filter(function (val) ).concat(arr2.filter(function (val) ));
console.log(difference); // [2, 4, 6, 7, 8, 9, nan, nan]
2. 利用es6中 array.from方法及set
function difference (a, b) )))
}var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, nan], arr2 = [1, 3, 5, nan];
var differenceval = difference (arr1, arr2);
console.log(differenceval ); // [2, 4, 6, 7, 8, 9]
3. 利用es7中新增的includes + filter
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, nan], arr2 = [1, 3, 5, nan];
var difference = arr1.concat(arr2).filter(function (val) );
console.log(difference); // [2, 4, 6, 7, 8, 9]
JS陣列求並集,交集和差集
es7 filter結合includes 並集 let union a.concat b.filter v a.includes v 1,2,3,4,5 交集 let intersection a.filter v b.includes v 2 差集 let difference a.concat ...
JS求陣列的交集 並集 差集
現有兩個陣列 let a 101,201,601 let b 201,301,801 複製 1.求交集 交集元素由既屬於集合a又屬於集合b的元素組成 方法1let intersection a.filter v b.includes v 201 複製 方法2 let intersection arr...
交集並集差集
1 內連線 select from student a inner join sc b on a.sno b.sno 左連線 select from student a left join sc b on a.sno b.sno 差集 select sno from student except s...