How to compare two JSON objects in JavaScript?

To compare two JSON objects in JavaScript, use _.isEqual function from lodash library.

First add this script somewhere in the head of your HTML:

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js" ></script>

Then you can start using _.isEqual function in your JavaScript like so:

const object1 = { Hello: 'World!', One: 'Two' };
const object2 = { Hello: 'World!' };
const object3 = { One: 'Two', Hello: 'World!' };

console.log(_.isEqual(object1, object2)); // false
console.log(_.isEqual(object1, object3)); // true