Method 1: Iterate through the array, create a new array, use indexOf to determine whether it exists in the new array, if not, push to the new array, and finally return the new array

function delre(ar) {

    var ret = [];

    for (var i = 0, j = ar.length; i < j; i++) {

        if (ret.indexOf(ar[i]) === -1) {

            ret.push(ar[i]);

        }

    }

    return ret;

}
Copy the code

Method 2: Iterate through the number group, use the object object to save the array value, determine whether the array value has been saved in the object, if not saved, push to a new array and object[arrayItem]=1 record saving

function delre(ar) { var tmp = {}, ret = []; for (var i = 0, j = ar.length; i < j; i++) { if (! tmp[ar[i]]) { tmp[ar[i]] = 1; ret.push(ar[i]); } } return ret; }Copy the code

Method 3: Array subscript judgment method, traverse the number group, use indexOf to determine whether the value of the element is equal to the current index, if equal, add

function delre(ar) { var ret = []; ar.forEach(function(e, i, ar) { if (ar.indexOf(e) === i) { ret.push(e); }}); return ret; }Copy the code

Method 4: array sort first, and then compare the two arrays of one end to remove the weight

function delre(ar) { var ret = [], end; ar.sort(); end = ar[0]; ret.push(ar[0]); for (var i = 1; i < ar.length; i++) { if (ar[i] ! = end) { ret.push(ar[i]); end = ar[i]; } } return ret; }Copy the code

Method 5, loop through the values in the quadratic array, compare them in turn, the most stupid method, but support object and array form deduplication

function delre(ar){ for(var i=0; i<ar.length; i++){ for(var j=i+1; j<ar.length; j++){ if(ar[j]==ar[i]){ ar.splice(j,1) j-- } } }Copy the code

Method six, ES6-set

[... new Set ([' 1 ', 2,3,4,1,2,12,2])]Copy the code

Method 7: Use key-value pairs – objects cannot be duplicated by the same name

Let a = [1,2,13,1,2,1,'1','1']; let a = [1,2,13,1, 1,'1','1']; const delre = arr => { var obj ={} arr.forEach(value => { obj[value]=0; }) return Object.keys(obj); } console.log(delre(a));Copy the code

Method 8: Use Reduce to remove weight

let obj = {}; arr = arr.reduce(function(item, next) { obj[next.key] ? '' : obj[next.key] = true && item.push(next); return item; } []);Copy the code

Method 9: Use filter to remove weight

delre = (arr)=>{ return arr.filter((e,i,arr)=>{ return arr.indexOf(e) == i; })}Copy the code

If there is any doubt, please contact, timely correction. Thank you very much!