ES7, ES6, ES5 seeking to achieve an array of union

1, ES7 implementation

ES7 new in a Array.prototype.includes (): includes () method is used to determine whether an array contains a specified value, in some cases, if it contains returns true, otherwise returns false. We can includes a method implemented in conjunction with the methods required filter array union, intersection, and difference, as follows:

let a = [1,2,3];
let b = [2,4,5];
//并集
console.log(a.concat(b.filter(v => !a.includes(v))));//[1, 2, 3, 4, 5]
//交集
console.log(a.filter(v => b.includes(v)));// [2]
//差集
console.log(a.concat(b).filter(v => a.includes(v) && !b.includes(v)));//[1, 3]

2, ES6 implementation (using the principle of de-emphasis of the array Set)

ES6 new in a Array.from (): This method creates a new instance of the array or from one array-like object may be iterative (an array of objects and classes may be about to traverse the object into an array with a length as long as the length of the array type, basically. It can be converted to an array). The method for us to achieve the structure incorporates an array Set union, intersection, and difference, as follows:

let a = [1,2,3];
let b = [2,4,5];

let aSet = new Set([1,2,3]);
let bSet = new Set([2,4,5]);

//并集
let union = Array.from(new Set(a.concat(b)));
console.log(union );//[1, 2, 3, 4, 5]
//交集
let intersection = Array.from(new Set(a.filter(v => bSet.has(v))));
console.log(intersection);//[2]
//差集
let differenceNew = Array.from(new Set(a.concat(b).filter(v => aSet.has(v) && !bSet.has(v))));
console.log(differenceNew);//[1,3]

3, ES5 implementation

IndexOf filter may be utilized and ES5 mathematical set operations, however, since the method indexOf NaN always returns -1, so the need for process compatibility, i.e. two cases:
(1) does not consider the case of a NaN (excluding array NaN)

var a = [1,2,3];
var b = [2,4,5];

// 并集
var union = a.concat(b.filter(function(v) {
    return a.indexOf(v) === -1
}))
// 交集
var intersection = a.filter(function(v){ 
    return b.indexOf(v) > -1 
}) 
// 差集
var difference = a.filter(function(v){ 
    return b.indexOf(v) === -1 
})
console.log(union); // [1,2,3,4,5]
console.log(intersection);// [2]
console.log(difference);// [1,3]

(2) Consider the case of NaN

var a = [1, 2, 3, NaN];
var b = [2, 4, 5];

var aHasNaN = a.some(function (v) {
    return isNaN(v)
})
var bHasNaN = b.some(function (v) {
    return isNaN(v)
})

// 并集
var union = a.concat(b.filter(function (v) {
    return a.indexOf(v) === -1 && !isNaN(v)
})).concat(!aHasNaN && bHasNaN ? [NaN] : []) 
// 交集
var intersection = a.filter(function (v) {
    return b.indexOf(v) > -1
}).concat(aHasNaN && bHasNaN ? [NaN] : []) 
// 差集
var difference = a.filter(function (v) {
    return b.indexOf(v) === -1 && !isNaN(v)
}).concat(aHasNaN && !bHasNaN ? [NaN] : [])

console.log(union);// [1,2,3,4,5,NaN]
console.log(intersection);// [2]
console.log(difference);//1,3,NaN
Published 149 original articles · won praise 166 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_44369568/article/details/100930917