es6 takes intersection, union, difference and complement of two arrays

1. Intersection

Let A and B be two sets. The set whose elements belong to both A and B is called the intersection (set) of A and B, and is denoted as A∩B (or B∩A).

A∩B={x|x∈A, and x∈B}

Method 1: filter + includes

// A∩B={x∣x∈A,且x∈B}
const arr1 = ['1', '2', '3', '4']
const arr2 = ['2', '4', '6', '8']
const arr = arr1.filter(e => arr2.includes(e))
console.log(arr) // ['2', '4']

Analysis: Filter the data contained in arr2 in arr1, which is the intersection.
 

Method 2: concat + filter + includes + Set

// A∩B={x∣x∈A,且x∈B}
const arr1 = ['1', '2', '3', '4']
const arr2 = ['2', '4', '6', '8']
const array = arr1.concat(arr2).filter(e => arr1.includes(e) && arr2.includes(e))
const arr = [...new Set(array)]
console.log(arr) // ['2', '4']

Analysis: First merge the two arrays, then filter out the data belonging to both arr1 and arr2, and finally remove the duplicates, which is the intersection.

2. Union

Let A and B be two sets. The set with elements belonging to A or B as elements is called the union (set) of A and B, and is denoted as A∪B (or B∪A).

A∪B={x∣x∈A, or x∈B}

Method 1: concat + Set

// A∪B={x∣x∈A,或x∈B}
const arr1 = ['1', '2', '3', '4']
const arr2 = ['2', '4', '6', '8']
const arr = [...new Set(arr1.concat(arr2))]
console.log(arr) // ['1', '2', '3', '4', '6', '8']

Analysis: Merge two arrays and then remove duplicates, which is the union.

Method 2: concat + reduce

// A∪B={x∣x∈A,或x∈B}
const arr1 = ['1', '2', '3', '4']
const arr2 = ['2', '4', '6', '8']
const arr = arr1.concat(arr2).reduce((pre, cur) => {
    if (!pre.includes(cur)) {
        pre.push(cur)
    }
    return pre
}, [])
console.log(arr) // ['1', '2', '3', '4', '6', '8']

Analysis: First merge the two arrays, then execute the reduce function, set the initial value of the result value to the empty array [], when the current data does not exist in the result value array, push the current data into the result value array, and finally obtain the result The value array is the union.

3. Difference set

Note that A and B are two sets, then the set composed of all elements that belong to A but not to B is called set A minus set B (or the difference between set A and set B). Similarly, for sets A and B, The set {x|x∈A, and x∉B} is called the difference set of A and B.

AB={x|x∈A, and x∉B}

BA={x|x∈B, and x∉A}

Method 1: concat + filter + includes

// A-B={x∣x∈A,且x∉B}
const arr1 = ['1', '2', '3', '4']
const arr2 = ['2', '4', '6', '8']
const arr = arr1.concat(arr2).filter(e => arr1.includes(e) && !arr2.includes(e))
console.log(arr) // ['1', '3']

 Analysis: First combine the two arrays, and then filter out those that are not in arr1 or arr2, which is the difference set of AB.

// B-A={x∣x∈B,且x∉A}
const arr1 = ['1', '2', '3', '4']
const arr2 = ['2', '4', '6', '8']
const arr = arr1.concat(arr2).filter(e => arr2.includes(e) && !arr1.includes(e))
console.log(arr) // ['6', '8']

Analysis: First merge the two arrays, and then filter out those that are not in arr2 or arr1, which is the difference set of BA.

4. Supplement

Denote A and U as two sets. The set composed of elements belonging to the complete set U but not to set A is called the complement of set A, denoted CuA, that is, CuA={x|x∈U, and x∉A}.

The complement of A and B is equal to the complement of A plus the complement of B

Method 1: concat + filter + includes + ...

const arr1 = ['1', '2', '3', '4']
const arr2 = ['2', '4', '6', '8']
const array = arr1.concat(arr2)
const arr = [...array.filter(e => !arr1.includes(e)), ...array.filter(e => !arr2.includes(e))]
console.log(arr) // ['6', '8', '1', '3']

Analysis: First merge the two arrays, then filter out the data that are not in arr1 and arr2 respectively, and merge the two arrays to form the complement set.

Method 2: filter + includes + ...

const arr1 = ['1', '2', '3', '4']
const arr2 = ['2', '4', '6', '8']
const arr = [...arr1.filter(e => !arr2.includes(e)), ...arr2.filter(e => !arr1.includes(e))]
console.log(arr) // ['1', '3', '6', '8']

Analysis: Filter out the data in arr1 that are not in arr2, and filter out the data in arr2 that are not in arr1. The two arrays are combined to form the complement set.

5. es6 method analysis

The concat()  method is used to merge two or more arrays. This method does not change the existing array, but returns a new array.

filter()Method creates a shallow copy of a portion of the given array containing all elements of the test implemented by the provided function.

includes() The method is used to determine whether an array contains a specified value. According to the situation, if it does, it will be returned  true, otherwise it will be returned  false.

The reduce()  method sequentially executes a reducer function provided by you on each element in the array   . Each time  the reducer is run  , the calculation results of the previous elements are passed in as parameters, and finally the results are summarized into a single return value.

Set  objects allow you to store unique values ​​of any type, whether primitive values ​​or object references.

...  Spread syntax, you can expand array expressions or strings at the syntax level during function calls/array construction; you can also expand object expressions in key-value terms when constructing literal objects . (Literals generally refer to [1, 2, 3] this {name: "mdn"} concise construction method)

Guess you like

Origin blog.csdn.net/yerongtao/article/details/127090698