美文网首页
js数组去重

js数组去重

作者: callPromise | 来源:发表于2020-10-27 16:16 被阅读0次

1. 利用filter方法

const arr = [2,736,15,9,15,8998]
arr.filter((element, index, self) => {
  return self.indexOf(element) === index
})

chrome执行结果如下


image.png

2. 利用reduce方法

reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。

[43,512,23,1,23,8,923].reduce((accumulator, currentValue, currentIndex, array) => {
  if (!accumulator.includes(currentValue)) {
    accumulator.push(currentValue)
  }
  return accumulator
}, [])
image.png

3. 利用ES6的Set

const ar = ['fwef', 'fwef', 87, 87, true, true, false, false, NaN, NaN, null, null, undefined, undefined ];
console.log(Array.from(new Set(ar)));
image.png

4.for循环结合indexOf

5.双层for循环

相关文章

  • 数组的去重和数组中对象的去重

    数组中对象去重 方式1 jq方式 方式2 原生js方式 普通数组的去重 方式1 普通的数组去重js 方式2 Se...

  • js数组去重、对象数组去重

    普通数组去重 一、普通数组去重 方法一:遍历数组法 方法二:排序法 方法三:对象法 对象数组去重 方法一:将对象数...

  • js数组去重

    Set结构去重 ES6 提供了新的数据结构 Set。它类似于数组,但是成员的值都是唯一的,没有重复的值。 向 Se...

  • JS数组去重

    方法1:两层for循环,外层循环原数组,内层循环时进行比较。 方法2:利用对象的属性不能相同的特点去重 方法3:利...

  • js数组去重

  • js数组去重

    1.利用对象的属性唯一性去重 2.利用es6的Set

  • js数组去重

  • js 数组去重

  • JS数组去重

    方法一:遍历数组,建立新数组,利用indexOf判断是否存在于新数组中,不存在则push到新数组,最后返回新数组 ...

  • js数组去重

    方法一 (es6 set方法,简单粗暴) 方法二 创建空数组,用indexOf方法检索,没有的话插入新数组中 方...

网友评论

      本文标题:js数组去重

      本文链接:https://www.haomeiwen.com/subject/hxjxvktx.html