深度递归比较:对象和属性

JavaScript: Deep comparison recursively: Objects and properties

本文关键字:属性 对象 递归 比较 深度      更新时间:2023-09-26

今天我读完了Eloquent JS的第4章,我正在努力理解如何在对象和它们的属性之间执行深度比较,特别是通过使用递归调用。我知道我的解决方案是相当幼稚和有点笨重,但我正试图把我的头围绕所有这些新东西,我还在学习!我很感激你在改进代码方面提供的任何提示和帮助,如果你能帮助我更好地理解需要发生的递归。提前感谢!

  • 问题(Eloquent JS第二版,第四章,练习4):

    写一个函数deepEqual,它接受两个值并返回true只有当它们是相同的值或具有相同的对象时属性的值与递归的值相比也相等调用deeequal .

测试用例:
var obj = {here: {is: "an"}, object: 2};
var obj1 = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj,obj1));
我的代码:

  function objectTester(x) {
      if (typeof x === 'object' && x !== null)
        return true;
    }
    function deepEqual(valOne, valTwo) {
      if (valOne === valTwo) return true;
      var comp1 = objectTester(valOne);
      var comp2 = objectTester(valTwo);
      if (comp1 === comp2) {
        var count1;
        var count2;
        for (var prop in valOne) {
            count1++
            return count1;
        }
        for (var prop in valTwo) {
            count2++
            return count2;
        }
        if (count1 === count2) {
        // This is where I'm getting stuck, not sure how I can recurisvely compare
        // two arguments that are to be compared. 
      }
    }

可能最简单的方法就是发布一个带有大量注释的函数。递归部分在接近底部,在每个:

传递给的函数中:
// Helper to return a value's internal object [[Class]]
// That this returns [object Type] even for primitives
function getClass(obj) {
  return Object.prototype.toString.call(obj);
}
/*
** @param a, b        - values (Object, RegExp, Date, etc.)
** @returns {boolean} - true if a and b are the object or same primitive value or
**                      have the same properties with the same values
*/
function objectTester(a, b) {
  // If a and b reference the same value, return true
  if (a === b) return true;
  // If a and b aren't the same type, return false
  if (typeof a != typeof b) return false;
  // Already know types are the same, so if type is number
  // and both NaN, return true
  if (typeof a == 'number' && isNaN(a) && isNaN(b)) return true;
  // Get internal [[Class]]
  var aClass = getClass(a);
  var bClass = getClass(b)
  // Return false if not same class
  if (aClass != bClass) return false;
  // If they're Boolean, String or Number objects, check values
  if (aClass == '[object Boolean]' || aClass == '[object String]' || aClass == '[object Number]') {
    return a.valueOf() == b.valueOf();
  }
  // If they're RegExps, Dates or Error objects, check stringified values
  if (aClass == '[object RegExp]' || aClass == '[object Date]' || aClass == '[object Error]') {
    return a.toString() == b.toString();
  }
  // Otherwise they're Objects, Functions or Arrays or some kind of host object
  if (typeof a == 'object' || typeof a == 'function') {
    // For functions, check stringigied values are the same
    // Almost certainly false if a and b aren't trivial
    // and are different functions
    if (aClass == '[object Function]' && a.toString() != b.toString()) return false;
    var aKeys = Object.keys(a);
    var bKeys = Object.keys(b);
    // If they don't have the same number of keys, return false
    if (aKeys.length != bKeys.length) return false;
    // Check they have the same keys
    if (!aKeys.every(function(key){return b.hasOwnProperty(key)})) return false;
    // Check key values - uses ES5 Object.keys
    return aKeys.every(function(key){
      return objectTester(a[key], b[key])
    });
  }
  return false;
}

对Date, RegExp, Error等的测试应该可能返回false,如果值/字符串不相同,然后落入属性检查,但只有当你认为有人可能将属性附加到Number对象时才这样做(使用Number对象非常罕见,更不用说添加属性了,但我猜可能会发生)。

在这里:

/*
** @param a, b        - values (Object, RegExp, Date, etc.)
** @returns {boolean} - true if a and b are the object or same primitive value or
**                      have the same properties with the same values
*/
function objectTester(a, b) {
  // If a and b reference the same value, return true
  if (a === b) return true;
  // If a and b aren't the same type, return false
  if (typeof a != typeof b) return false;
  // Already know types are the same, so if type is number
  // and both NaN, return true
  if (typeof a == 'number' && isNaN(a) && isNaN(b)) return true;
  // Get internal [[Class]]
  var aClass = getClass(a);
  var bClass = getClass(b)
  // Return false if not same class
  if (aClass != bClass) return false;
  // If they're Boolean, String or Number objects, check values
  if (aClass == '[object Boolean]' || aClass == '[object String]' || aClass == '[object Number]') {
    if (a.valueOf() != b.valueOf()) return false;
  }
  // If they're RegExps, Dates or Error objects, check stringified values
  if (aClass == '[object RegExp]' || aClass == '[object Date]' || aClass == '[object Error]') {
    if (a.toString() != b.toString()) return false;
  }
  // For functions, check stringigied values are the same
  // Almost impossible to be equal if a and b aren't trivial
  // and are different functions
  if (aClass == '[object Function]' && a.toString() != b.toString()) return false;
  // For all objects, (including Objects, Functions, Arrays and host objects),
  // check the properties
  var aKeys = Object.keys(a);
  var bKeys = Object.keys(b);
  // If they don't have the same number of keys, return false
  if (aKeys.length != bKeys.length) return false;
  // Check they have the same keys
  if (!aKeys.every(function(key){return b.hasOwnProperty(key)})) return false;
  // Check key values - uses ES5 Object.keys
  return aKeys.every(function(key){
    return objectTester(a[key], b[key])
  });
  return false;
}

您可以使用下面的代码进行深度比较-

const isEqual = (a, b) => {
  if (a === b) return true;
  if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
  if (!a || !b || (typeof a !== 'object' && typeof b !== 'object')) return a === b;
  if (a === null || a === undefined || b === null || b === undefined) return false;
  if (a.prototype !== b.prototype) return false;
  let keys = Object.keys(a);
  if (keys.length !== Object.keys(b).length) return false;
  return keys.every(k => isEqual(a[k], b[k]));
};

示例-

isEqual({ prop1: [2, { e: 3 }], prop2: [4], prop3: 'foo' }, { prop1: [2, { e: 3 }], prop2: [4], prop3: 'foo' }); // true

检查下面的代码。这应该对你有用。

function objectEquals(x, y) {
    'use strict';
    if (x === null || x === undefined || y === null || y === undefined) { return x === y; }
    // after this just checking type of one would be enough
    if (x.constructor !== y.constructor) { return false; }
    // if they are functions, they should exactly refer to same one (because of closures)
    if (x instanceof Function) { return x === y; }
    // if they are regexps, they should exactly refer to same one (it is hard to better equality check on current ES)
    if (x instanceof RegExp) { return x === y; }
    if (x === y || x.valueOf() === y.valueOf()) { return true; }
    if (Array.isArray(x) && x.length !== y.length) { return false; }
    // if they are dates, they must had equal valueOf
    if (x instanceof Date) { return false; }
    // if they are strictly equal, they both need to be object at least
    if (!(x instanceof Object)) { return false; }
    if (!(y instanceof Object)) { return false; }
    // recursive object equality check
    var p = Object.keys(x);
    return Object.keys(y).every(function (i) { return p.indexOf(i) !== -1; }) &&
        p.every(function (i) { return objectEquals(x[i], y[i]); });
}

我觉得所有的答案都很棒。但是对于不太专业的人来说,我想让阅读变得更容易,澄清使用Array.prototype.every的解决方案如何用于嵌套数组。事实证明,全局对象的keys方法有一个不太常用的功能,即接收数组作为参数和

返回一个数组,其元素是与直接在object上找到的可枚举属性。

console.log(typeof [] === 'object') // true
Object.keys([true, { a:'a', b: 'b' }, 2, null]) // ['0', '1', '2', '3']

所以这就是Object.keys().every()如何很好地用于函数参数,索引{}s和[]s,使用属性键或数组索引。

希望能有所帮助