单数组
let list = [1,2,3,4,5,5,5,6]
let newList = Array.from(new Set(list))
console.log('newList >> ', newList);
let list = [1,2,3,4,5,5,5,6]
let newList = []
list.forEach((item) => {
if (!newList.includes(item)) {
newList.push(item)
}
})
console.log('newList >>> ', newList)
let list = [1,2,3,4,5,5,5,6]
let newList = []
let map = new Map()
list.forEach((item) => {
if (!map.has(item)) {
map.set(item, ture)
newList.push(item)
}
})
console.log('newList >>> ', newList)
let list = [1,2,3,4,5,5,5,6]
let newList = []
let map = new Map()
list.forEach((item) => {
if (newList.indexOf(item) === -1) {
newList.push(item)
}
})
console.log('newList >>> ', newList)
let list = [1,2,3,4,5,5,5,6]
for (let i = 0; i < list.sort().length; i++) {
if (list[i] == list[i + 1]) {
list.splice(i, 1)
i--
}
}
console.log('newList >>> ', list)
let list = [1,2,3,4,5,5,5,6]
for (let i = 0; i < list.sort().length; i++) {
for (let j = i + 1; j < list.sort().length; j++) {
if (list[i] == list[j]) {
list.splice(i, 1)
j--
}
}
}
console.log('newList >>> ', list)
let list = [1,2,3,4,5,5,5,6]
function callBack(index) {
if (index >= 1) {
if (list[index] === list[index - 1]) {
list.splice(index, 1)
}
callBack(index - 1)
}
}
callBack(list.sort().length - 1)
console.log('newList >>> ', list)
对象数组
function nonRepet(list, key) {
return list.filter((item) => !map.has(item[key].toString()) && map.set(item[key].toString()))
}
console.log('newList >>> ', nonRepet(list, 'id'));
function nonRepet(list, key) {
let list = [];
return arr.filter((item) => !list.includes(item.name) && list.push(item.name)
}
console.log('newList >>> ', nonRepet(list, 'id'));
let newArray = new Set();
list.filter(function(item, index, array) {
newArray.add(item.name)
});
if(newArray.size != list.length){
console.log("有重复值");
}