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

Posted 努力努力再努力

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手动实现深比较两个数组或两个对象是否完全相等相关的知识,希望对你有一定的参考价值。

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;

 

以上是关于手动实现深比较两个数组或两个对象是否完全相等的主要内容,如果未能解决你的问题,请参考以下文章

用JavaScript比较两个数组是否相等

JS比较两个数组是否相等 是否拥有相同元素

python is 与==区别

JavaScript如何比较两个数组的内容是否相同

.NET如何快速比较两个byte数组是否相等

判断两个数组是不是相等