How to Compare Two Objects in JavaScript

There are the following methods to compare two objects in JavaScript.

  1. Using the “JSON.stringify()” method
  2. Using the lodash Library’s isEqual() function

Method 1: Using the JSON.stringify() function

The “JSON.stringify()” is a built-in JavaScript method that can convert an object into a string, and then comparing strings is more accessible than reaching the whole object.

Syntax

JSON.stringyfy(objectName);

Arguments

It takes an object name as the parameter. Then, the stringify() method converts the entire object into a string data type and then compares the two strings as a string. However, it should be noted that for two different objects to be treated as equal, all the key-value pairs should be equal. Otherwise, the comparison will return false.

On the other hand, another function called isEqualconsidersact that within two different objects, even if the order of the key-value pair is not the same, they are equal.

Return value

It returns boolean values, which can either be true or false. For example, if two objects are equal, the output is shown as boolean true, and if the objects are not equal, the output is false.

Example

const f1 = {
 fruit: "Banana"
};

const f2 = {
 fruit: "Banana"
};

// Using JavaScript
let isFruitEqual = (JSON.stringify(f1) === JSON.stringify(f2));
console.log(isFruitEqual); // true

Output

true

In this example, we created two objects, f1, and f2, with the same key-value pair. Then we used the JSON.stringify() function and compare both objects. As a result, we are getting true and printing using the console.log() function.

Method 2: Using lodash’s isEqual() function

The _.isEqual() function compares two JavaScript Objects.

Syntax

_.isEqual(obj1, obj2);

Arguments

The isEqual() function has two parameters, obj1, and obj2. So these are two objects that we need to compare.

Return value

It returns the Boolean (true or false) value.

Example

import _ from "lodash"

const f1 = {
 fruit: "Banana"
};

const f2 = {
 fruit: "Banana"
};

// Using JavaScript
let isFruitEqual = _.isEqual(f1, f2);
console.log(isFruitEqual);

Output

true

We created two objects, f1, and f2, with the same key-value pair in this example.

Here we used the isEqual() function provided by the lodash library to compare f1 and f2 objects. Finally, the result we are printing using the console.log() function.

Nested comparison

Using the isEqual() function also, we can perform nested comparisons.

const fruitOne = {
   fruit: 'Banana',
   nutrients: {
   energy: '300kJ',
   minerals: {
     name: 'protein',
   },
  },
};

const fruitTwo = {
  fruit: 'Mango',
  nutrients: {
  energy: '310kJ',
  minerals: {
    name: 'Carbs',
  },
 },
};

// Using JavaScript
let isFruitEqual = _.isEqual(fruitOne, fruitTwo);
console.log(isFruitEqual);

Output

false

Which method is better to use?

Usually, the key-value pairs are not in the sorted order because they do not need such order or sorting as in arrays, lists, etc., where sorting helps in many operations.

The JSON.stringyfy() will have to transverse the whole object to make the comparison.

On the other hand, the loadash will need less time in most cases because even if there is one mismatching, the function will end the current comparison and return false.

That’s it.

Leave a Comment