I was recently interviewed an array to re-do a headache, now specifically to say for everyone to use several methods:
First, the use ES6 Set deduplication (ES6 most commonly used)
function unique (arr) { return Array.from(new Set(arr)) } var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}]; console.log(unique(arr)) //[1, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {}, {}]
Do not consider the compatibility of this method code to the minimum weight.
Second, for use for nesting, and then to re-splice (most commonly used in the ES5)
function UNIQUE (ARR) { for ( var I = 0; I <arr.length; I ++ ) { for ( var J = I +. 1; J <arr.length; J ++ ) { IF (ARR [I] == ARR [ J]) { // first equivalent to a second, splice delete the second method arr.splice (J,. 1 ); J - ; } } } return ARR; } var ARR = [1,1, ' to true ',' to true ', to true , to true , 15, 15, to false , to false, Undefined, undefined, null , null , NaN3, NaN3, 'NaN3', 0, 0, 'A', 'A' , {}, {}]; the console.log (UNIQUE (ARR)) // [. 1, "true", 15, false, undefined, NaN, NaN, "NaN", "a", {...}, {...}] // NaN {} and not to heavy, two null directly disappear
Double loop, the outer loop element, the inner loop when the comparison value. Values are the same, this value is omitted.
Third, the use includes (ES6 Method)
function unique(arr) { if (!Array.isArray(arr)) { console.log('type error!') return } var array =[]; for(var i = 0; i < arr.length; i++) { if( !array.includes( arr[i]) ) {//includes 检测数组是否有某个值 array.push(arr[i]); } } return array } var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}]; console.log(unique(arr)) //[1, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {…}, {…}] //{}没有去重
Fourth, the use sort ()
function unique(arr) { if (!Array.isArray(arr)) { console.log('type error!') return; } arr = arr.sort() var arrry= [arr[0]]; for (var i = 1; i < arr.length; i++) { if (arr[i] !== arr[i-1]) { arrry.push(arr[i]); } } return arrry; } var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}]; console.log(unique(arr)) // [0, 1, 15, "NaN", NaN, NaN, {…}, {…}, "a", false, null, true, "true", undefined] //NaN、{}没有去重
Using sort () sorting method, and then traversing the element ratio according to a result of adjacent sorted.
Fifth, the use of filter
function UNIQUE (ARR) { return arr.filter ( function (Item, index, ARR) { // the current element, the first index in the original array == current index value, otherwise the current element return arr.indexOf (Item , 0) === index; }); } var ARR = [1,1, 'to true', 'to true', to true , to true , 15, 15, to false , to false , undefined, undefined, null , null , NaN3, NaN3, 'NaN3', 0, 0, 'A', 'A' , {}, {}]; the console.log (UNIQUE (ARR)) // [. 1, "to true", to true, 15, to false, undefined , null, "NaN", 0 , "a", {...}, {...}]