评论

数组去重常用方法

数组去重

function DuplicateRemovalArray_1(arr) {
    var resultArr;
    resultArr = arr.filter(function (item, index, array) {
        return array.indexOf(item) == index;
 
    });
    return resultArr;
}
 
function DuplicateRemovalArray_2(arr) {
    /**
     *     Accumulator (acc) (累计器)
            Current Value (cur) (当前值)
            Current Index (idx) (当前索引)
            Source Array (src) (源数组)
     *
     */
 
    let result = arr.sort().reduce((accumulator, currentValue, currentIndex, array) => {
        if (accumulator.length === 0 || accumulator[accumulator.length - 1] !== currentValue) {
            accumulator.push(currentValue);
        }
        return accumulator;
    }, []);
 
    return result;
}
 
function DuplicateRemovalArray_3(arr) {
    let mapArray = Array.from(new Set(arr));
 
    return mapArray;
 
}

 
let p1 = DuplicateRemovalArray_1([1,2,2,3]);
let p2 = DuplicateRemovalArray_2([3,2,6,6,6,3]);
let p3 = DuplicateRemovalArray_3([1,1,0,1,5]);
 
console.log(p1);
console.log(p2);
console.log(p3);


最后一次编辑于  09-05  
点赞 0
收藏
评论

1 个评论

  • 小明🐵
    小明🐵
    09-06

    牛逼

    09-06
    赞同
    回复