Deep Object Compare using Recursion

Deep Object Compare using Recursion

how to compare two json objects in javascript
deep compare array of objects javascript
how to compare two json objects in angular 6
typescript compare objects
deep-equal
lodash compare objects
angular 6 compare objects
deep equality

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));

Deep Comparison exercise explanation - Anastasia Z, Now with recursive function we compare the value of the properties. In this case values are objects {is: “an”}. We can't use strict equal operator to compare them. Questions: I am looking for a C# specific , open source (or source code available) implementation of recursive, or deep, object comparison. I currently have two graphs of live objects that I am looking to compare to each other, with the result of the comparison being a set of discrepancies in the graphs.


You can't return from a forEach loop, and you have to return the recursive call:

function deepObjCompare(obj1, obj2) {

  let keys = Object.keys(obj1);
  return keys.every((key) => { // use a for-of loop instead of forEach
    const key1 = obj1[key];
    const key2 = obj2[key];

    if (key2) {
      if (typeof key1 === "object" && key1 !== null) {
         return deepObjCompare(key1, key2); // return the 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: "Bill",address: "Paris",age: 39};

console.log("Are Obj1 and Obj3 equal? ",deepObjCompare(obj1, obj3));
console.log("Are Obj1 and Obj2 equal? ",deepObjCompare(obj1, obj2));

recursion in deepEqual() on object graphs with cycles · Issue #434 , then compare the two parsed objects, with a deep equals routine. Using a custom stringify function like this, which ignores circular references:. If you're using a functional language like C, recursion might be faster. If you're using an imperative language like Java, iteration is probably faster. 2:how compareTo() method checks if strings are equal


There are actually a number of things I see that are wrong with this implementation. But as to why it's returning undefined - that's because your function never actually returns anything. All your return statements are inside the function passed to forEach, and therefore do nothing with regard to the outer deepObjCompare.

JavaScript object deep comparison. Comparing x === y, where x , //Deep compare objects Why don't you just use JSON.stringify on both objects and compare the This may be necessary if the method is called recursively. Given a deeply-nested list such as: it is often useful to visualise it as a tree written out as follows: In order to explore this whole tree, that is to visit every sublist of a list, we need to use deep-recursion. A function is deeply recursive if it is called recursively on the car of a list as well as on its cdr.


Deep comparison of two objects - JavaScript, Hello Campers,. Can anyone explain me how the solution for deep comparison of two objects works? I know it is recursion. What I cannot  Therefore, I propose an alternative for the use of ExpandoObject to hold inner dictionaries by making use of Method Recursion. Using the Code. The first method is responsible for passing JSON Object as string and returns fully deserialized object in dictionary. Please note that this is an extension method to the datatype String.


recursive-deep-equal-object, JS, Rust, Swift, Python (with Turtle), Basic (beta), R, Bash, Quil, Crystal, Julia, Elixir, Nim, Dart, Reason NodeJs, Erlang, TypeScript, Pygame, Love2D, Tkinter,  The objects to compare can be of different types but must have the same properties/fields. For example if actual object has a name String field, it is expected the other object to also have one. If an object has a field and a property with the same name, the property value will be used over the field.


JavaScript deep object comparison, stringify based comparison in terms of performance and behavior. Warning: JSON.stringify may return different strings for two deep equal objects. Briefly, Objects Comparer is an object-to-object comparer, which allows the developer to compare objects recursively, member-by-member, and define custom comparison rules for certain properties