Using JavaScript's properties of object, we can easily implement an array of duplicates to be removed.
The attribute of object is: key must be unique.
Remove the array duplicates:
1 Convert an array to an object, the value of the array as the key of the object
Because key is unique, object does not add a key when it encounters a duplicate array value
2 Convert Object object to array, key is the value of the array.
In the previous repeating array, each value actually corresponds to an object with only one key, so that when you revert to the array, the duplicate value is removed
1 /*Remove duplicates of an array with the attributes of object:2 1 Convert an array into an Oject object, using the value of the array as the key of obj3 2 Convert an object to an array4 */5 6 //passing in an array to return object objects7 varToobj =function(arr) {8obj = {};9 for(varTempincharr) {Ten //Each value of arr is added to the key,value of obj is true OneObj[arr[temp]] =true; A } - returnobj; - }; the //Incoming obj object returns an array of arr - varToarr =function(obj) { - vararr = []; - for(varTempinchobj) { + //push all the keys into the array - Arr.push (temp); + } A returnarr; at }; - //unite the two methods. - varGetuniq =function(arr) { - returnToarr (Toobj (arr)); - }; - in - vararr = [1,1,2,2,3,3,4,4,5,5,5,6,6,6,6,6,6,5,4,3,54,8,11]; to +Alert (Getuniq (arr));
Use JavaScript to remove duplicates in an array