To check whether two objects are equal or not in javascript, You will have to learn the best approach to check if two objects have the same data. Unlike primitive data types (such as numbers and strings), It is not possible to compare objects using the equality operator (==
or ===
).
To accurately check if two objects are equal in JavaScript, you will learn the possible ways one by one with some examples
Ways to Check Two Objects are Equal using JavaScript
1. Using a Custom Function
This defines a function, `objectsAreEqual`, to compare two objects based on their key-value pairs.
It checks if they have the same keys and values. If they do, it returns `true`; otherwise, it returns `false`.
The code is tested with two example objects, and it correctly identifies whether they are equal or not.
function objectsAreEqual(obj1, obj2) { const keys1 = Object.keys(obj1); const keys2 = Object.keys(obj2); if (keys1.length !== keys2.length) { return false; } for (const key of keys1) { if (obj1[key] !== obj2[key]) { return false; } } return true; } const object1 = { name: 'John', age: 30 }; const object2 = { name: 'John', age: 30 }; const object3 = { name: 'Alice', age: 25 }; console.log(objectsAreEqual(object1, object2)); // true console.log(objectsAreEqual(object1, object3)); // false
Steps to write code
- Declare a Function: Create a function called objectsAreEqual to compare two objects for equality.
- Get Object Keys: Obtain the keys of the first object and store them in keys1. Do the same for the second object, storing its keys in keys2.
- Check Key Length: Compare the lengths of keys1 and keys2. If they are not equal, immediately return `false` to indicate the objects are not equal.
- Iterate Through Keys: Loop through each key in keys1.
- Compare Values: For each key, compare the values associated with that key in both objects (obj1[key] and obj2[key]). If you find a key where the values are not equal, return false to indicate inequality.
- Return True: If the loop completes without finding any unequal key-value pairs, return true to indicate that the objects are equal.
- Example 1: Compare object1 and object2. If they have the same keys and values, return true.
- Example 2: Compare object1 and object3. If they have differing values for any keys, return false.
Using JSON Stringification
In this code, you’re comparing the stringified versions of the objects (string1
, string2
, and string3
) instead of comparing the objects directly
const object1 = { name: 'John', age: 30 }; const object2 = { name: 'John', age: 30 }; const object3 = { name: 'Alice', age: 25 }; const string1 = JSON.stringify(object1); const string2 = JSON.stringify(object2); const string3 = JSON.stringify(object3); console.log(string1 === string2); // true console.log(string1 === string3); // false
- Create Objects: Define three JavaScript objects named `object1`, `object2`, and `object3`, each with key-value pairs.
- Stringify Objects: Use the `JSON.stringify()` method to convert `object1`, `object2`, and `object3` into JSON strings, storing the results in `string1`, `string2`, and `string3`.
- Compare Strings: Use the equality operator (`===`) to compare `string1` and `string2`. If they represent the same JSON content, this comparison will yield `true`.
- Output Result: Output the result of the comparison of `string1` and `string2` to the console. In this case, it will print `true`.
- Compare Strings Again: Use the equality operator (`===`) to compare `string1` and `string3`. This comparison checks if `string1` and `string3` represent the same JSON content.
- Output Result Again: Output the result of the comparison of `string1` and `string3` to the console. In this case, it will print `false`.
Using Lodash isEqual
In this updated code, you’re using the isEqual
function from the lodash library to compare the equality of the objects object1
, object2
, and object3
.
const _ = require('lodash'); const object1 = { name: 'John', age: 30 }; const object2 = { name: 'John', age: 30 }; const object3 = { name: 'Alice', age: 25 }; console.log(_.isEqual(object1, object2)); // true console.log(_.isEqual(object1, object3)); // false
- Import Lodash: Import the Lodash library and assign it to the variable `_`.
- Create Objects: Define three JavaScript objects named `object1`, `object2`, and `object3`, each with key-value pairs.
- Compare `object1` and `object2`: Use the `_.isEqual()` function from Lodash to compare the equality of `object1` and `object2`. This function examines if these two objects have identical keys and values. It returns `true`, indicating that `object1` and `object2` are considered equal.
- Output First Comparison Result: Output the result of the comparison between `object1` and `object2` to the console. In this case, it will print `true`.
- Compare `object1` and `object3: Use `_.isEqual()` again to compare `object1` and `object3`. This comparison checks if these two objects have the same keys and values. Since they differ in values for the “name” and “age” keys, the function returns `false`, indicating that `object1` and `object3` are not equal.
- Output Second Comparison Result: Output the result of the comparison between `object1` and `object3` to the console. In this case, it will print `false`.