数组去重ES5和ES6

ES5

let arr = [12, 13, 14, 12, 15, 14];
let newArr = []
for (let i = 0; i < arr.length; i++) {
    var current = arr[i];
    if (newArr.indexOf(current) === -1) {
        newArr.push(current)
    }
}
console.log(newArr);

ES6

let arr = [12, 13, 14, 12, 15, 14];

let newArr2 = new Set(arr);
console.log(newArr2)

你可能感兴趣的:(面试题,javascript,javascript,前端,面试)