1.数组去重

//es5数组去重
function unique(arr){
    var res =[];
    for(var i=0,len=arr.length;i<len;i++){
        var obj = arr[i];
        if(res.indexOf(obj)===-1) res.push(obj);
    }
    return res;
}

//es6数组去重
var array = [1, 2, 1, 1, '1'];
function unique(array) {
   return Array.from(new Set(array));
}
console.log(unique(array)); // [1, 2, "1"]

//再简化下:
function unique(array) {
    return [...new Set(array)];
}
//再简化下:
var unique = (a) => [...new Set(a)]

2.数组两个元素交换


export const swapArray = (arr, index1, index2)=>{
    arr[index1] = arr.splice(index2, 1, arr[index1])[0];
    return arr;
}

//es6

var a = 1;
var b = 2;
[a,b] = [2,1]