zoukankan      html  css  js  c++  java
  • 去重

    Set去重( sort() 是排序, 不要搞混了)

    let ary = [1, 12, 3, 24, 1, 3, 2, 8, 3];
    let p = [...new Set(ary)]
    console.log(p)

    数组去重(封装方法,挂在原型上,方法不改变原有数组)

    1.判断插入 (我最喜欢,简单易懂)

    var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
    Array.prototype._unrepeat= function () {
      return this.filter((item, index, arr) => {
        return arr.indexOf(item) === index; // 有就返回第一个索引
      })
    }
    console.log(arr._unrepeat()) // 和 ...new Set() 一样,不会去除重复的{}

    一下方法可供参考,打开思路

    2.对象键值对处理

    Array.prototype.keysRD = function () {
      //=>this:ary 我们需要操作的数组,如果不想改变原有的数组,我们需要把要操作的数组克隆一份一模一样的处理,处理的都是克隆的这个数组
      let _this = [...this],
        obj = {};
      for (let i = 0; i < _this.length; i++) {
        let item = _this[i];
        if (typeof obj[item] !== 'undefined') {
          //=>当前迭代的这一项在数组中已经存在,我们把这一项在数组中干掉
          // _this.splice(i, 1); [后面项移位,消耗性能]
          _this[i] = _this[_this.length - 1];
          _this.length--;
          i--;
          continue;
        }
        obj[item] = true;
      }
      obj = null;
      return _this;
    };
    
    // 使用:
    let ary = [1, 2, 3, 2, 3, 4, 3, 2, 2, 2, 2, 3, 4, 5, 6, 7, 4, 1, 3, 2];
    let uniqueAry = ary.keysRD();
    console.log(uniqueAry); // [1, 2, 3, 4, 5, 6, 7]

    3.双循环(不推荐)

    Array.prototype.doubleLoopRD = function () {
        let _this = [...this];
        for (let i = 0; i < _this.length; i++) {
            let item = _this[i];
            //=>每一次迭代到ITEM后,都拿其后面的内容和它进行比较(出现和当前项相同的,我们就在数组中把其干掉)
            for (let j = i + 1; j < _this.length; j++) {
                if (item === _this[j]) {
                    //=>删除索引J这一项
                    _this[j] = _this[_this.length - 1];
                    _this.length--;
                    j--;
                }
            }
        }
        return _this;
    };

    4.增减去重

    indexOf:获取当前项在数组中第一次出现位置的索引,也能判断是否存在这一项(不存在获取的索引是-1),这个方法是不兼容IE6~8的
    Array.prototype.indexOfRD = function () {
        let _this = [...this];
        //=>依次迭代数组中的每一项,验证当前项在数组中是否存在(不是和整个数组比较是否存在,而是和当前项的后面项比较是否存在=>类似于双FOR),存在把当前项干掉
        for (let i = 0; i < _this.length; i++) {
            let item = _this[i],
                nextAry = _this.slice(i + 1);
            if (nextAry.indexOf(item) > -1) {
                _this[i] = _this[_this.length - 1];
                _this.length--;
                i--;
            }
        }
        return _this;
    };

    5.排序后相邻去除法

    先把数组进行排序,验证当前项和后一项是否相同,如果不相同,说明没有重复,我们把着于相提取出来保存即可
    Array.prototype.myUnique = function () {
        let _this = [],
            ary = this.slice(0).sort((a, b) => a - b);
        for (let i = 0; i < ary.length; i++) {
            let item = ary[i],
                next = ary[i + 1];
            if (item !== next) {
                _this.push(item);
            }
        }
        return _this;
    };

    // 使用 let ary
    = [1, 2, 3, 2, 3, 4, 3, 2, 2, 2, 2, 3, 4, 5, 6, 7, 4, 1, 3, 2]; let uniqueAry = ary.myUnique(); console.log(uniqueAry);
  • 相关阅读:
    1304. 和为零的N个唯一整数
    557. 反转字符串中的单词 III
    集群Eureka构建步骤
    单机Eureka构建步骤——08端口服务和8001端口服务注册进Eureka
    服务注册中心——Eureka基础知识
    存活的cloud
    系统中重复部分打包成一个jar包供其他工程使用(工程重构)
    cloud-consumer-order80微服务消费者订单Module模块
    cloud-provider-payment8001微服务提供者支付Module模块
    学习SpringCloud——项目工程搭建
  • 原文地址:https://www.cnblogs.com/MrZhujl/p/13130971.html
Copyright © 2011-2022 走看看