利用indexOf方法,es6也可以用includes方法
const array = [1, 2, 3, 4, 2, 3, "e", "e"];
const newArray = [];
array.forEach(item => {
if (!newArray.includes(item)) {
newArray.push(item);
}
})
利用对象键值
const array = [1, 2, 3, 4, 2, 3, "e", "e"];
const obj = {}
const newArray = [];
array.forEach(item => {
if (obj[item]) {
return
} else {
obj[item] = true;
newArray.push(item);
}
})
利用set的不可重复性 (感觉比较简单)
const array = [1, 2, 3, 4, 2, 3, "e", "e"];
const set = new Set(array);
const newArray = [...set];
网友评论