Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Compare keys & values in a JSON object when one object has extra keys in JavaScript
When comparing JSON objects in JavaScript, you often need to check if the common keys have matching values, even when one object has extra properties. This is useful for validating partial object matches or checking if one object is a subset of another.
Problem Statement
Consider these two objects:
const obj1 = {a: "apple", b: "banana", c: "carrot"};
const obj2 = {a: "apple", e: "egg", b: "banana", c: "carrot", d: "dog"};
We need a function that returns true because all common keys (a, b, c) have matching values, ignoring the extra keys (e, d) in obj2.
However, if the common keys have different values:
const obj1 = {a: "apple", b: "banana", c: "carrot"};
const obj2 = {a: "ant", e: "egg", b: "banana", c: "carrot", d: "dog"};
The function should return false because the key "a" has different values ("apple" vs "ant").
Solution Using Object.entries()
const obj1 = {
a: "apple",
b: "banana",
c: "carrot"
};
const obj2 = {
a: "apple",
b: "banana",
c: "carrot",
d: "dog",
e: "egg"
};
const obj3 = {a: "apple", b: "banana", c: "carrot"};
const obj4 = {a: "ant", e: "egg", b: "banana", c: "carrot", d: "dog"};
function checkEquality(a, b) {
const entries1 = Object.entries(a);
const entries2 = Object.entries(b);
const short = entries1.length > entries2.length ? entries2 : entries1;
const long = short === entries1 ? b : a;
const isEqual = short.every(([k, v]) => long[k] === v);
return isEqual;
}
console.log(checkEquality(obj1, obj2)); // Should return true
console.log(checkEquality(obj3, obj4)); // Should return false
true false
How It Works
The function works by:
- Converting both objects to key-value arrays using
Object.entries() - Identifying the shorter object (fewer properties)
- Using
every()to check if all keys from the shorter object exist in the longer object with matching values - Returning
trueonly if all common keys have identical values
Alternative Approach Using Object.keys()
function compareCommonKeys(obj1, obj2) {
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
const shorterKeys = keys1.length <= keys2.length ? keys1 : keys2;
const otherObj = shorterKeys === keys1 ? obj2 : obj1;
return shorterKeys.every(key => otherObj[key] === (shorterKeys === keys1 ? obj1[key] : obj2[key]));
}
console.log(compareCommonKeys(obj1, obj2));
console.log(compareCommonKeys(obj3, obj4));
true false
Key Points
- The comparison ignores extra keys in the larger object
- All common keys must have identical values for a
trueresult - The function works regardless of which object has more properties
- Uses strict equality (
===) for value comparison
Conclusion
This approach efficiently compares objects with different numbers of properties by focusing only on common keys. It's particularly useful for validating partial data matches or checking object subset relationships.
