Comparing Two Objects for Equivalent Property Values in JavaScript

Last Updated : 18 Aug, 2026

In this problem, we check whether all key-value pairs of obj2 are present in obj1 with the same values.

  • Compares the keys and values of two objects.
  • Returns true when every key-value pair in obj2 exists in obj1 with the same value.
  • Returns false if a key is missing or its corresponding value is different.

Approach 1: Using for...in Loop

The idea is to iterate through the keys of obj2 and check whether each key exists in obj1 with the same value. If any key-value pair differs, the function returns false; otherwise, it returns true.

Example: Checks whether all key-value pairs of obj2 are present in obj1.

JavaScript
let obj1 = {
    name: "John",
    age: 23,
    degree: "CS"
};

let obj2 = {
    age: 23,
    degree: "CS"
};

function check(obj1, obj2) {

    // Iterate through keys of obj2
    for (const key in obj2) {

        // Check whether key and value match
        if (obj1[key] !== obj2[key]) {
            return false;
        }
    }

    return true;
}

console.log(check(obj1, obj2));

Output
true

Approach 2: Using Object.keys() and every()

The idea is to use Object.keys() to get all keys of obj2 and then use every() to check whether each key exists in obj1 with the same value.

Example: Uses Object.keys() and every() to compare the key-value pairs.

JavaScript
// Define the first object
let obj1 = {
    name: "John",
    age: 23,
    degree: "CS"
}

// Define the Second object
let obj2 = {
    age: 23,
    degree: "CS"
}

// Define the function check
function check(obj1, obj2) {
    return Object

        // Get all the keys in array
        .keys(obj2)
        .every(val => obj1.hasOwnProperty(val)
            && obj1[val] === obj2[val])
}
 
console.log(check(obj1, obj2))

Output
true

Approach 3: Using JSON.stringify()

The idea is to convert both objects into JSON strings using JSON.stringify() and compare the resulting strings. This approach works when the objects have the same properties in the same order and contain JSON-compatible values.

Example: Compares two objects by converting them into JSON strings.

JavaScript
function areObjectsEqual(obj1, obj2) {

    // Convert objects to JSON strings
    const stringifiedObj1 = JSON.stringify(obj1);
    const stringifiedObj2 = JSON.stringify(obj2);

    // Compare the strings
    return stringifiedObj1 === stringifiedObj2;
}

let obj1 = {
    name: "John",
    age: 23,
    degree: "CS"
};

let obj2 = {
    name: "John",
    age: 23,
    degree: "CS"
};

console.log(areObjectsEqual(obj1, obj2));

Output
true

Note: JSON.stringify() is sensitive to property order, so objects containing the same properties in a different order may produce different strings.

Approach 4: Using a Custom Function

The idea is to first compare the number of keys in both objects. If the lengths differ, the objects cannot be equal. Then, each key and its corresponding value are compared.

Example: Checks whether both objects contain exactly the same keys and values.

JavaScript
function areObjectsEqual(obj1, obj2) {

    const keys1 = Object.keys(obj1);
    const keys2 = Object.keys(obj2);

    // Check if the number of keys is different
    if (keys1.length !== keys2.length) {
        return false;
    }

    // Compare each key-value pair
    for (const key of keys1) {
        if (!Object.hasOwn(obj2, key) ||
            obj1[key] !== obj2[key]) {
            return false;
        }
    }

    return true;
}

let obj1 = {
    name: "John",
    age: 23,
    degree: "CS"
};

let obj2 = {
    name: "John",
    age: 23,
    degree: "CS"
};

console.log(areObjectsEqual(obj1, obj2));

Output
true

Approach 5: Using Object.entries() – O(N) Time and O(N) Space

The idea is to use Object.entries() to obtain key-value pairs from both objects. We first compare the number of entries and then check whether every key-value pair in the first object exists in the second object.

Example: Compares objects using their key-value pairs obtained through Object.entries().

JavaScript
function areObjectsEqual(obj1, obj2) {

    const entries1 = Object.entries(obj1);
    const entries2 = Object.entries(obj2);

    // Check if the number of entries differs
    if (entries1.length !== entries2.length) {
        return false;
    }

    // Compare each key-value pair
    for (const [key, value] of entries1) {
        if (!Object.hasOwn(obj2, key) ||
            obj2[key] !== value) {
            return false;
        }
    }

    return true;
}

let obj1 = {
    name: "John",
    age: 23,
    degree: "CS"
};

let obj2 = {
    name: "John",
    age: 23,
    degree: "CS"
};

console.log(areObjectsEqual(obj1, obj2));

Output
true

Approach 6: Using Map Object

The idea is to convert both objects into Map objects using Object.entries(). We then compare the keys and their corresponding values.

Example: Converts objects into Map objects and compares their key-value pairs.

JavaScript
let obj1 = {
    name: "John",
    age: 23,
    degree: "CS"
};

let obj2 = {
    age: 23,
    degree: "CS"
};

function check(obj1, obj2) {

    const map1 = new Map(Object.entries(obj1));
    const map2 = new Map(Object.entries(obj2));

    for (const [key, value] of map2) {

        // Check whether key and value match
        if (!map1.has(key) || map1.get(key) !== value) {
            return false;
        }
    }

    return true;
}

console.log(check(obj1, obj2));

Output
true

Note: This approach performs a partial comparison, checking whether all key-value pairs of obj2 exist in obj1.

Approach 7: Using Lodash _.isMatch()

The idea is to use Lodash's _.isMatch() method to check whether obj1 contains all the key-value pairs specified in obj2. It performs a partial comparison rather than requiring both objects to have exactly the same keys.

Syntax:

_.isMatch(object, source);

Example: Checks whether obj2 is a matching subset of obj1.

JavaScript
const _ = require('lodash');

let obj1 = {
    name: "John",
    age: 23,
    degree: "CS"
};

let obj2 = {
    age: 23,
    degree: "CS"
};

// Check whether obj2 matches obj1
const isMatch = _.isMatch(obj1, obj2);

console.log(isMatch);

Output:

true

Note: _.isMatch() checks whether the properties and values in obj2 are present in obj1; extra properties in obj1 are allowed.

Comment