数组去重

利用对象的key来辅助

const array = [11, 2, 3, 6, 2, 9, 23, 2, 8, 6];
const res = {}, newArr = [];
for (let index = 0; index < array.length; index++) {
    const element = array[index];
    if (res.hasOwnProperty(element)) {
        res[element].push(index);
    } else {
        res[element] = [];
        newArr.push(element)
    }
}
console.log('原来的老数组:',array);
console.log('去重后的数组:',newArr);
const repeatDic = Object.entries(res).filter(item=>item[1].length>0);
console.log('重复的key:', repeatDic.map(item=>item[0]));
console.log('重复的位置:',Object.values(res).flat());

原文地址:https://www.cnblogs.com/dshvv/p/15238937.html