Array deduplication 12 options
Let’s go full circle and summarize our array methods:
- pop
- push
- shift
- unshift
- slice
- splice
- sort
- reverse
- concat
- join
- indexOf
- lastIndexOf
- map
- forEach
There are several other ways to handle arrays
The -includes: method is used to determine whether an array contains a specified value, returning true if it does and false otherwise.
– find: Returns the first found item
-some: Returns a Boolean value, true as long as one is true
-every: Returns a Boolean value. True is returned only when each item is true
– filter: Returns a new array after filtering. If it returns true, it is retained, and false is filtered out
– reduce: converges
Below we enter the topic ~ (hope can be helpful to you ~ small make up a little skin!! Hahahahahaha)
Method 1: Set: is not a data type, is a data structure; Members of the only
letArr =,1,12,3,1,88,66,9,66 [12];function unique(ary) {
lets = new Set(ary); / / Array. The from: willsetData structures turn into real arrays;return Array.from(s)
}
unique(arr);
Copy the code
Method 2: Object attribute names must be unique
letArr =,1,12,3,1,88,66,9,66 [12];function unique(ary) {
let obj = {};
for(leti=0; i<ary.length; i++){let cur = ary[i];
if(obj[cur]){ //ary.splice(i,1); Ary [I]=ary[ary.length-1]; ary.length--; // delete the last item I --;continue; } obj[cur]=cur; // add a key pair to obj; Attribute name and attribute value are the same}} unique(arr);Copy the code
Method 3: indexOf
letArr =,1,12,3,1,88,66,9,66 [12];function unique(ary) {
let newAry = [];
for(leti=0; i<ary.length; i++){let cur = ary[i];
if(newAry.indexOf(cur)===-1){ newAry.push(cur); }}return newAry;
}
unique(arr)
Copy the code
Method 4: sort
letArr =,1,12,3,1,88,66,9,66 [12];function unique(ary) {
let a = ary.sort(function (a,b) {
return a-b;
});
for(leti=0; i<a.length; i++){if(a = = = [I] a [I + 1]) {a.s plice (I + 1, 1); i--; }}return a;
}
unique(arr)
Copy the code
Method 5: includes. Returns true if the array contains that item; Does not contain return false;
letArr =,1,12,3,1,88,66,9,66 [12];function unique(ary) {
let newAry = [];
let len = ary.length;
for(leti=0; i<len; i++){let cur = ary[i];
if(!newAry.includes(cur)){
newAry.push(cur);
}
}
return newAry;
}
console.log(unique(arr));
Copy the code
HasOwnProperty: Check if the property name is a private property of the object. Returns a Boolean value;
letArr =,1,12,3,1,88,66,9,66 [12];function unique(ary) {
let obj = {};
return ary.filter(function(item,index,a) {// item: each member of the array // index: the corresponding index of the member // a: the entire array // hasOwnProperty to check whether the attribute is present;return obj.hasOwnProperty(typeof item+item)?false:obj[typeof item+item]=true;
if(obj.hasOwnProperty(typeof item+item)){
return false
}else{
obj[typeof item+item]=true;
return true;
}
})
}
console.log(unique(arr))
Copy the code
Method 7: Filter +indexOf
letArr =,1,12,3,1,88,66,9,66 [12];function unique(ary) {
return ary.filter(function (item,index,a) {
return ary.indexOf(item)===index;
})
}
console.log(unique(arr));
Copy the code
8. Splice
letArr =,1,12,3,1,88,66,9,66 [12];function unique(ary) {
for(leti=0; i<ary.length; i++){for(j=i+1; j<ary.length; j++){if(ary[i]===ary[j]){ ary.splice(j,1); j--; }}}return ary;
}
unique(arr);
Copy the code
Method 9: Recursion
letArr =,1,12,3,1,88,66,9,66 [12];function unique(ary) {
let len= ary.length;
ary = ary.sort(function (a,b) {
return a-b;
});
function loop(index) {
if(index>=1){
if(ary[index]===ary[index-1]){
ary.splice(index,1);
}
loop(index-1)
}
}
loop(len-1);
return ary;
}
console.log(unique(arr));
Copy the code
Method 10: Map: uses the characteristics of Map data structure to store values;
letArr =,1,12,3,1,88,66,9,66 [12];function unique(ary) {
let newAry =[];
let map = new Map();
for(leti=0; i<ary.length; i++){if(! map.has(ary[i])){ map.set(ary[i],true);
newAry.push(ary[i]);
}
}
}
unique(arr);
Copy the code
Method 11: Reduce
letArr =,1,12,3,1,88,66,9,66 [12];functionUnique (ary) {// reduce: The first is the function, and the second argument is passed to the prev of the first callback;returnAry. Reduce ((prev,next)=>{// The return value of this function is the next prev;returnprev.includes(next)? prev:[...prev,next]; },[]) } console.log(unique(arr));Copy the code
Method 12: A set similar to method 1, using the residual operator…
letArr =,1,12,3,1,88,66,9,66 [12];let a = [...new Set(arr)];
console.log(a);
Copy the code