js数组去重(9种方法)搬运自:前端菜鸟好先森

本文详细介绍了九种不同的JavaScript数组去重技巧,包括使用Set、includes、map、indexOf、for循环、双层循环、递归以及针对对象数组的filter和Map方法,适合开发者提升数组处理能力。
摘要由CSDN通过智能技术生成

 

原创地址:js数组去重(9种方法),你都会了吗?-CSDN博客

仅搬运!!!防丢失!!! 

以下共有九种数组去重的方式和详解(包含对象数组去重):

1.利用Array.from(new Set)去重:

// 1.利用set去重 
// Set是es6新增的数据结构,似于数组,但它的一大特性就是所有元素都是唯一的,没有重复的值,我们一般称为集合
// Array.from()就是将一个类数组对象或者可遍历对象转换成一个真正的数组,也是ES6的新增方法
    let list = ['你是最棒的', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的',]
    let newList = Array.from(new Set(list))
    console.log('newList', newList);

效果: 

2.利用includes去重

//2.利用Array.includes 去重   
//includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false。
let list = ['你是最棒的1', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的1',]
    let newList2 = []
    list.forEach((item) => {
        // 空数组newList2 不包含item为false ,取反为true 执行数组添加操作
        // 如果数组包含了 item为true 取反为false 不执行数组添加操作
        if (!newList2.includes(item)) {
            newList2.push(item)
        }
    })
    console.log('newList2', newList2);

效果

 3.利用map去重

//3.利用map去重
//map数据结构是es6中新出的语法,其本质也是键值对,只是其键不局限于普通对象的字符串 
let list = ['你是最棒的2', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的2',]
    let newList3 = [];
    let map = new Map()
    list.forEach((item) => {
     // 如果map.has指定的item不存在,那么就设置key和value 这个item就是当前map里面不存在的key,把这个item添加到新数组
     // 如果下次出现重复的item,那么map.has(item等于ture 取反 !map.has(item)  不执行
        if (!map.has(item)) {
            map.set(item,ture)
            newList3.push(item)
        }
    })
    console.log('newList3', newList3);

 

效果:

4.利用indexOf去重 

//4.利用indexOf去重  
//indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。如果没有找到匹配的字符串则返回 -1 
let list = ['你是最棒的3', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的3',]
    let newList4 = [];
    list.forEach((item) => {
     // 空数组newList4第一次循环没有找到匹配的item 返回-1  执行数组添加操作
     // 如果数组在第n次循环中找到了newList4数组中item 例如:item等于6 而在newList4数组中已经有9 所以indexOf就不等于-1  不执行数组添加操作
        if (newList4.indexOf(item) === -1) {
            newList4.push(item)
        }
    })
    console.log('newList4', newList4);

效果:

5. 利用单层for循环去重

// 5.单层for循环去重 
// Array.splice() 方法用于添加或删除数组中的元素。会改变原数组 
let list = ['你是最棒的4', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的4',]
    let newlist5 = [];
    for (let i = 0; i < list.sort().length; i++) {
        if (list[i] == list[i + 1]) {
            list.splice(i, 1)
            i--
        }
    }
    console.log('newlist5', list);

 效果:

6.利用双层for循环去重 

// 6.双层for循环去重
// Array.splice() 方法用于添加或删除数组中的元素。会改变原数组
let list = ['你是最棒的5', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的5',]
    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('newlist6', list);

 

效果:

 7.利用递归去重

// 7.递归去重
let list = ['你是最棒的6', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的6',]
    function callBack(index) {
      // 数组的长度不能等于0
        if (index >= 1) {
      // 第一次如果数组最后一个索引和最后第二个索引相等
            if (list[index] === list[index - 1]) {
      // 那么就删除这个索引的元素
                list.splice(index, 1)
            }
      // 继续调用这个函数 第一次传入的参数是数组末尾第一个 第二次传入的参数是数组末尾第二个 层层递减
            callBack(index - 1)
        }
    }
      //传入排序好的数组的最大索引index
    callBack(list.sort().length - 1)
    console.log('newList7', list);

 效果:

8.利用Array.filter和map对象数组去重 (性能较高)

//8.利用Array.filter和map去重 
let map = new Map()
let list3 = [{
        name: '好先森1',
        id: 1
    },
    {
        name: '好先森1',
        id: 2
    },
    {
        name: '好先森2',
        id: 3
    },
    {
        name: '好先森3',
        id: 3
    }
    ]
    // 对象数组去重
    function xxx(list3, key) {
        return list3.filter((item) => !map.has(item[key].toString()) && map.set(item[key].toString()))
    }
    console.log('newList8', xxx(list3, 'id'));

 效果:

9.利用Array.filter和Array.includes 对象数组去重

// 9.利用Array.filter和Array.includes去重
let list4 = [{
        name: '好先森1',
        id: 1
    },
    {
        name: '好先森1',
        id: 2
    },
    {
        name: '好先森2',
        id: 3
    },
    {
        name: '好先森3',
        id: 3
    }
    ]
    function xxx(arr) {
        let list = [];
        return arr.filter((item) => !list.includes(item.name) && list.push(item.name))
    }
    console.log('newList9', xxx(list4));

 效果:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值