zoukankan      html  css  js  c++  java
  • 手动实现深比较两个数组或两个对象是否完全相等

    let arrayEquals = (array1, array2) => {
      // if array1 or array2 is a falsy value, return
      if (!array1 || !array2)
        return false;
      // compare lengths - can save a lot of time
      if (array1.length != array2.length)
        return false;
      for (var i = 0, l = array1.length; i < l; i++) {
        // Check if we have nested arrays
        if (array1[i] instanceof Array && array2[i] instanceof Array) {
          // recurse into the nested arrays
          if (!arrayEquals(array1[i],array2[i] ))
            return false;
        } else if (array1[i] instanceof Object && array2[i] instanceof Object){
          // 比较含有的对象是否相等
          if (!objectEquals(array1[i],array2[i]))
            return false;
        }
        else if (array1[i] != array[i]) {
          console.log("else if")
          // Warning - two different object instances will never be equal: {x:20} != {x:20}
          return false;
        }
      }
      return true;
    }
    let objectEquals = (object1, object2) => {
      //For the first loop, we only check for types
      for (let propName in object1) {
        //Check for inherited methods and properties - like .equals itself
        //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
        //Return false if the return value is different
        if (object1.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
          return false;
        }
        //Check instance type
        else if (typeof object1[propName] != typeof object2[propName]) {
          //Different types => not equal
          return false;
        }
      }
      //Now a deeper check using other objects property names
      for(let propName in object2) {
        //We must check instances anyway, there may be a property that only exists in object2
        //I wonder, if remembering the checked values from the first loop would be faster or not
        if (object1.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
          return false;
        } else if (typeof object1[propName] != typeof object2[propName]) {
          return false;
        }
        //If the property is inherited, do not check any more (it must be equa if both objects inherit it)
        if(!object1.hasOwnProperty(propName))
          continue;
        
        //Now the detail check and recursion
        
        //This returns the script back to the array comparing
        /**REQUIRES Array.equals**/
        if (object1[propName] instanceof Array && object2[propName] instanceof Array) {
          // recurse into the nested arrays
          if (!arrayEquals(object1[propName], object2[propName]))
            return false;
        }
        else if (object1[propName] instanceof Object && object2[propName] instanceof Object) {
          // recurse into another objects
          //console.log("Recursing to compare ", object1[propName],"with",object2[propName], " both named ""+propName+""");
          if (!objectEquals(object1[propName],object2[propName]))
            return false;
        }
        //Normal value comparison for strings and numbers
        else if(object1[propName] != object2[propName]) {
          return false;
        }
      }
      //If everything passed, let's say YES
      return true;
    }
    let equals = {
      arrayEquals,
      objectEquals
    }
    window.equals = equals;
    module.exports = equals;
  • 相关阅读:
    Ceres求解直接法BA实现自动求导
    栈溢出漏洞原理详解与利用
    【机器学习】使用CNN神经网络实现对图片分类识别及模型转换
    【Android】java中调用JS的方法
    5 项大奖,70 项满分!阿里云全方位引领云原生技术升级
    分布式系统架构与云原生—阿里云《云原生架构白皮书》导读
    Dubbo-go 发布 1.5 版,朝云原生迈出关键一步
    阿里产品专家:高情商的技术人,如何做沟通?
    CNCF 新增两个孵化项目 | 云原生生态周报 Vol. 58
    掌门教育微服务体系 Solar | 阿里巴巴 Nacos 企业级落地上篇
  • 原文地址:https://www.cnblogs.com/gwf93/p/10209639.html
Copyright © 2011-2022 走看看