Method 1 (do not use Set)
unique = (array) => { const hash = [] for(let i=0; i<array.length; i++){ hash[array[i]] = true } const result = [] for(let j in hash){ result.push(j) } return result }Copy the code
Disadvantages: Supports only numeric or string arrays, if the array contains objects, such as array = [{number:1}, 2], error
Method 2 (using Set)
Unique = (array) => {return [...new Set(array)] // return array. from(new Set(array))}Copy the code
Cons: API is too new to be supported by older browsers
Method 3 (Using Map)
unique = (array) => { let map = new Map(); let result = [] for (let i = 0; i < array.length; I ++) {if(map.has(array[I])) {continue} else {// If the key does not exist in the map, Set (array[I], true); result.push(array[i]); } } return result; }Copy the code
Cons: API is too new to be supported by older browsers