How to compare two objects in Javascript

How to compare two objects in Javascript

What is an object?

An Object in Javascript is a data type that comes in very handy when needing a more complex data structure.

Compared to the other data types, objects don’t have any specific type — but rather they are used as containers for collections of key-value pairs where the value could be any of the primitive data types.

```let object = { key: value }

Types of equality

1 - Referential equality


```let user1 = {
  name:"Frank",
  surname:"Smith"
}
let user2 = {
  name:"Frank",
  surname:"Smith"
}
let user3 = user1
console.log(user1 === user2)  // => false
console.log(user1 === user3)  // => true

Whenever we create a variable to store a specific data type, we create a position in memory that we can refer to whenever needed.

In case we compare the primitive types with the equality operator === , javascript will compare the value stored in those positions in memory. In the case of objects — the situations will be different — as javascript will check for the reference equality, not value equality, so testing if they have the same instance rather than the value.

2 - Shallow equality

```function shallowEquality(object1, object2) { const entries1 = Object.entries(object1); const entries2 = Object.entries(object2); if (entries1.length !== entries2.length) { return false; } for (let i = 0; i < entries1.length; ++i) { // Keys if (entries1[i][0] !== entries2[i][0]) { return false; } // Values if (entries1[i][1] !== entries2[i][1]) { return false; } }

return true; } let user1= { name:"Frank", surname:"Smith", userInformations: { id:"userId" } } let user2 = { name:"Frank", surname:"Smith", userInformations: { id:"userId" } } shallowEquality(user1, user2) // => false

As we can see in the previous example, the comparison returned false even though the values are the same. With shallow equality, we check if each key-value pair has the same value, but we will not be able to compare nested values.

3 - Deep equality


```function deepEqual(object1, object2) {
  const keys1 = Object.keys(object1);
  const keys2 = Object.keys(object2);
  if (keys1.length !== keys2.length) {
    return false;
  }
  for (const key of keys1) {
    const value1 = object1[key];
    const value2 = object2[key];
    const areObjects = isValidObject(value1) &&       isValidObject(value2);
    if (
      areObjects && !deepEqual(val1, val2) ||
      !areObjects && val1 !== val2
    ) {
      return false;
    }
  }
  return true;
}
function isValidObject(object) {
 return object !== null && typeof object === "object" ? true : false 
}
let user1= {
  name:"Frank",
  surname:"Smith",
  userInformations: {
    id:"userId"
  }
}
let user2 = {
  name:"Frank",
  surname:"Smith",
  userInformations: {
    id:"userId"
  }
}
deepEquality(user1, user2) // => true

With the deep equality, we check if each key-value pair has the same value and allow us to check also nested values.

Other methods to check for equality

Rather than write our functions to check each value of an object and compare it to another one, we can use different ways to do so like the one listed below:

JSON.stringify()

```let user1= { name:"Frank", surname:"Smith", userInformations: { id:"userId" } } let user2 = { userInformations: { id:"userId" }
name:"Frank", surname:"Smith", } console.log(JSON.stringify(user1) === JSON.stringify(user2)) // => false

We can compare two objects by transforming them into JSON objects.
That will allow us to use the equality operator === and check if the
key-value pair are equal. In this case, we only need to make sure that the order of the values in the object are exactly in the same order, otherwise it’ll fail the comparison.

Lodash(._isEqual)


```let user1= {
  name:"Frank",
  surname:"Smith",
  userInformations: {
    id:"userId"
  }
}
let user2 = {
  userInformations: {
    id:"userId"
  }  
  name:"Frank",
  surname:"Smith",
}
console.log(_.isEqual(user1, user2)); // true

We can also use some external packages like Lodash, that will make it easier for us to compare objects without creating custom functions — to make sure that we check for value equality regardless the order or nested values.