码迷,mamicode.com
首页 > 编程语言 > 详细

手动实现深比较两个数组或两个对象是否完全相等

时间:2019-01-02 19:32:55      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:can   fir   this   using   methods   相等   with   sed   ever   

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;

 

手动实现深比较两个数组或两个对象是否完全相等

标签:can   fir   this   using   methods   相等   with   sed   ever   

原文地址:https://www.cnblogs.com/gwf93/p/10209639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!