数组去重

时间:2019-04-03 13:25:50   收藏:0   阅读:146
const arr = [1, 2, 1, 4, 5, 6, 7, 4];
const newArr = arr.filter((currentVlaue, index, arr) => {
    return index === arr.indexOf(currentVlaue);
})
function unique(arr) {
    var res = [arr[0]];
    for (var i = 1; i < arr.length; i++) {
        var repeat = false;
        for (var j = 0; j < res.length; j++) {
            if (arr[i] == res[j]) {
                repeat = true;
                break;
            }
        }
        if (!repeat) {
            res.push(arr[i]);
        }
    }
    return res;
}

原文:https://www.cnblogs.com/Yancyzheng/p/10648162.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!