• 数组去重我总结的最常用的方法,其他不常用就不写了


    mounted(){
        // 数组去重测试
        // 1.set  es6的  性能比for循环高一点性能no2
        let arr=[10,8,7,5,1,2,3,8,7,11]
        console.log(arr,arr.length,'初始数组');
        // console.log(Array.from(new Set(arr)));//  Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组。
    
        // 2.for 循环去重(思路就是先排序后比较)性能no3
        // let aa= arr.sort()
        // let cc=[]
        // console.log(aa);//[1, 10, 11, 2, 3, 5, 7, 7, 8, 8]
        // for(let i=0;i<aa.length;i++){
        //   if(aa[i]!=aa[i+1]){
        //     cc.push(aa[i])
        //   }
        // }
        // console.log(cc);//[1, 10, 11, 2, 3, 5, 7, 8]搞定去重
        /* 
        for...of 语句创建一个循环来迭代可迭代的对象。在 ES6 中引入的 for...of 循环,以替代 for...in 和 forEach() ,并支持新的迭代协议。
        for...of 允许你遍历 Arrays(数组), Strings(字符串), Maps(映射), Sets(集合)等可迭代的数据结构等
        不能便利对象,可以array.from转成数组在用for of
        */
    
    /*     //3 for...of + Object 性能最高 性能no1  
    // 首先创建一个空对象,然后用 for 循环遍历,利用对象的属性不会重复这一特性,校验数组元素是否重复
        let result = []
        let obj = {}
        for (let i of arr) {
            if (!obj[i]) {
                result.push(i)
                obj[i] = '我是属性的值'
                // obj[i] = '随便你想写什么就写什么啊'
                console.log(obj,'00000000000')  
              // 打印一下就知道了啊,如obj={10:1,8:1类似于name:'lbj',name:'wade'利用对象属性唯一性去重}} 
            }
        }
        console.log(result,'66666666666');//[10, 8, 7, 5, 1, 2, 3, 11]搞定 */
    
            //4 for 循环 然后includes  性能撇 不推荐 只是提供思路
            let gg=[]
            for (let i=0;i<arr.length;i++) {
              if(!gg.includes(arr[i])){
                gg.push(arr[i])
              }
            }
            console.log(gg,'gg');
            
    
      }

    https://www.cnblogs.com/wisewrong/p/9642264.html  这个写了耗时的可以看一下

    推荐new Set  和对象唯一性去重 不解释

  • 相关阅读:
    openresty 使用 log_by_lua 发送日志到 syslog-ng
    uuid 了解
    基于openresty 的几个开发框架
    openresty 几个插件使用
    kong 了解
    openresty && hashids&& redis 生成短链接
    kong k8s 安装 以及可视化管理界面
    hashids 了解
    Apache Tez 了解
    Cascalog了解
  • 原文地址:https://www.cnblogs.com/myfirstboke/p/11228277.html
Copyright © 2020-2023  润新知