数组的去重方式

73 阅读1分钟

1.使用filter和indexof

   let arr  = [1,45,8,9,1,45,8]
    /**
     * filter 三个参数的含义
     * val- 当前的子级
     * index- 当前的下标
     * dataArray 原始数组
     */
    //filter
    let newArr=arr.filter((val,index,dataArray)=>{
        return dataArray.indexOf(val)===index
    })

2.使用set数据结构处理

console.log(Array.from(new Set(arr)))