Search code examples
javascriptrecursionarrow-functions

Deep Object Compare using Recursion


I'm attempting deep object comparison using recursion, but my function is returning undefined.

I know there are better ways to compare objects (IE JSON.Stringify()) but I'm not understanding why my function is returning undefined.

function deepObjCompare(obj1, obj2) {

  Object.keys(obj1).forEach((key) => {
    const key1 = obj1[key];
    const key2 = obj2[key];

    if (key2) {
      if (typeof key1 === "object" && key1 !== null) {
        deepObjCompare(key1, key2);
      } else if (key1 === key2) {
        return true;
      }
    }
    return false;
  });
}

const obj1 = {
  name: "Bill",
  address: {
    cityNow: "Paris",
    cityFuture: "NYC"
  },
  age: 39
};

const obj2 = {
  name: "Bill",
  address: {
    cityNow: "Paris",
    cityFuture: "NYC"
  },
  age: 39
};

const obj3 = {
  name: "Bob",
  address: "Paris",
  age: 39
};

console.log(deepObjCompare(obj1, obj3));


Solution

  • I see three main issues here:

    • deepObjCompare lacks a return statement, which is why it is implicitly returning undefined.
    • The .forEach method always returns undefined, so you'll want to change that to another method that will return an actual value. I think .every is what you're after here.
    • You do not return the result of your recursive call.

    All together, that would change your snippet to the following:

    function deepObjCompare(obj1, obj2) {
    
      return Object.keys(obj1).every((key) => { // Use .every and return the result!
        const key1 = obj1[key];
        const key2 = obj2[key];
    
        if (key2) {
          if (typeof key1 === "object" && key1 !== null) {
            return deepObjCompare(key1, key2); // Return the result of your recursive call!
          } else if (key1 === key2) {
            return true;
          }
        }
        return false;
      });
    }
    
    const obj1 = {
      name: "Bill",
      address: {
        cityNow: "Paris",
        cityFuture: "NYC"
      },
      age: 39
    };
    
    const obj2 = {
      name: "Bill",
      address: {
        cityNow: "Paris",
        cityFuture: "NYC"
      },
      age: 39
    };
    
    const obj3 = {
      name: "Bob",
      address: "Paris",
      age: 39
    };
    
    console.log(deepObjCompare(obj1, obj3)); // false
    console.log(deepObjCompare(obj1, obj2)); // true