• js中数组去重的几种方法


    数组去重的方法

    /**
     * new Array()里面如果只有一位数字就是创建新数组 设置数组的长度为几位  如果里面的数据多于一位就是创建新数组添加这几位数据
     * Array.from()方法从一个类数组或者可迭代对象创建一个新的浅拷贝的数组
     * Array.from(arrayLike[, mapFn[, thisArg]])
     * arrayLike想要转化的伪数组或者可迭代对象
     * mapFn一个回调函数 新数组中的每一项都会执行该回调
     * thisArg 执行回调时候的this对象
     */
    let arr=Array.from(new Array(1000),(x,index)=>{
      //index是新数组的索引
      return index
    })
    
    1. 利用filter方法+indexOf
    let arr=["1","1",1,1,false,false,true,true,undefined,undefined,{},{},99,99]
    
    function unique (arr) {
      return arr.filter((item,index,arr)=>{
        //当前元素在数组中的索引等于当前的索引值
        //如果不等的话证明当前元素已经存在数组中的
        return arr.indexOf(item) ===index
      })
    }
    
    
    let ary=unique(arr)
    console.log(ary)//["1", 1, false, true, undefined, {…}, {…}, 99]
    
    1. 利用es6的Set()去重
    let arr=["1","1",1,1,false,false,true,true,undefined,undefined,{},{},99,99]
    
    function unique (arr) {
      return Array.from(new Set(arr))
    }
    
    let ary=unique(arr)
    console.log(ary)//(8) ["1", 1, false, true, undefined, {…}, {…}, 99]
    
    1. 双重for循环splice去重
    let arr=["1","1",1,1,false,false,true,true,undefined,undefined,{},{},99,99]
    
    function unique (arr) {
      for(let i=0;i<arr.length;i++){
      for(let j=i+1;j<arr.length;j++){
        if(arr[i] ===arr[j]){
          arr.splice(j,1)
          j--
        }
      }
      }
      return arr
    }
    let ary=unique(arr)
    console.log(arr)//(8) ["1", 1, false, true, undefined, {…}, {…}, 99]
    
    1. 利用sort()
    let arr=["1","1",1,1,false,false,true,true,undefined,undefined,{},{},99,99]
    
    function unique (arr) {
      //李荣sort排序,根据排序后的结果遍历对比相邻的元素
      arr=arr.sort()
      let array=[arr[0]]
      for(let i=1;i<arr.length;i++){
        if(arr[i] !==arr[i-1]){
          array.push(arr[i])
        }
      }
      return array
    }
    
    let ary=unique(arr)
    console.log(ary)//(8) ["1", 1, false, true, undefined, {…}, {…}, 99]
    
    1. 利用对象的键值对中的键不能重复
    let arr=["1","1",1,1,false,false,true,true,undefined,undefined,{},{},99,99]
    
    function unique (arr) {
      let array=[]
      let obj={}
      for(let i=0;i<arr.length;i++){
        if(!obj[arr[i]]){
          array.push(arr[i])
          obj[arr[i]]=1
        }
      }
      return array
    }
    
    let ary=unique(arr)
    console.log(ary)//(6) ["1", false, true, undefined, {…}, 99]
    
    1. 利用includes是否包含某个值判断
    let arr=["1","1",1,1,false,false,true,true,undefined,undefined,{},{},99,99]
    
    function unique (arr) {
      //includes()方法判断一个数组中有没有包含指定的值 如果包含返回true 不包含返回false
      let array=[]
      for(let i=0;i<arr.length;i++){
        if(!array.includes(arr[i])){
          array.push(arr[i])
        }
      }
      return array
    }
    
    let ary=unique(arr)
    console.log(ary)//(8) ["1", 1, false, true, undefined, {…}, {…}, 99]
    
    1. 利用递归去重
    let arr=["1","1",1,1,false,false,true,true,undefined,undefined,{},{},99,99]
    
    function unique (arr) {
      let array=arr
      let length=array.length
      function loop (index) {
        if(index>=1){
          if(array[index]===array[index-1]){
            array.splice(index,1)
          }
          loop(index-1)
        }
      }
      loop(length-1)
      return array
    }
    let ary=unique(arr)
    console.log(ary)//[ '1', 1, false, true, undefined, {}, {}, 99 ]
    
    1. 利用reduce()方法
    let arr=["1","1",1,1,false,false,true,true,undefined,undefined,{},{},99,99]
    
    function unique (arr) {
      return arr.reduce((pre,cur)=>{
        console.log(pre,cur)
       return  pre.includes(cur)?pre:[...pre,cur]
      },[])
    }
    
    let ary=unique(arr)
    console.log(ary)//[ '1', 1, false, true, undefined, {}, {}, 99 ]
    
  • 相关阅读:
    leetcode(c++)(扫描线)
    深度学习手撕系列整理(待完善)
    leetcode(c++)(单调序列)
    leetcode(c++)(滑动窗口)
    leetcode(c++)(前缀和)
    【数据结构之字典树Trie】C语言实现
    微信小程序页面跳转如何传递对象参数
    动态规划
    数据结构(五)图图的两种遍历(深度优先和广度优先)
    红黑树(RebBlackTree)
  • 原文地址:https://www.cnblogs.com/my466879168/p/12715379.html
Copyright © 2020-2023  润新知