1、借助ES6提供的Set结构 new Set() 简单好用 强烈推荐
直接给一个新的数组里面,利用es6的延展运算符

2、利用 filter() 去重
filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。item是当前元素的值,index是当前元素的索引值。indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。利用indexOf() 查询到数组的下标,看是否等于当前的下标,相等的话就返回,否则不返回值。

3、利用for 循环 搭配 indexOf 去重

4、将数组的每一个元素依次与其他元素做比较,发现重复元素,删除 比较繁琐 不推荐

5、借助新数组 通过 indexOf 方法判断当前元素在数组中的索引,如果与循环的下标相等则添加到新数组中

6、利用双重for循环

7、利用includes实现数组去重

以上就是比较常用的七种方法了,有不懂的留言吧。
网友评论