首页 > 编程语言 > 详细

数组去重

时间:2019-04-03 13:25:50      阅读:138      评论:0      收藏:0      [点我收藏+]
  • 通过数组方法 filer来实现
    filter方法中,有三个参数,分别是currentValue 当前元素的值,index 当前元素的索引值,arr 当前元素属于的数组对象;
const arr = [1, 2, 1, 4, 5, 6, 7, 4];
const newArr = arr.filter((currentVlaue, index, arr) => {
    return index === arr.indexOf(currentVlaue);
})
  • for循环实现
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)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!