
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Compare keys & values in a JSON object when one object has extra keys in JavaScript
Suppose, we have two JSON objects like these −
const obj1 = {a: "apple", b: "banana", c: "carrot"}; const obj2 = {a: "apple", e: "egg", b: "banana", c: "carrot", d: "dog"};
We are required to write a JavaScript function that takes in two such objects. We want to be able to have a Boolean check comparing the two objects without having to remove data from either one.
For example, if I were to use the data above, the Boolean check should return true because the values of the keys that are in both objects match.
However, let’s say obj1 stays the same but obj2 is the following −
const obj1 = {a: "apple", b: "banana", c: "carrot"} const obj2 = {a: "ant", e: "egg", b: "banana", c: "carrot", d: "dog"}
With this data, it should return false because a key's value is not matching even though other fields are matching and some fields are not present in both objects.
Example
The code for this will be −
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 ? entries2 : entries1; const long = short === entries1 ? b : a; const isEqual = short.every(([k, v]) => long[k] === v); return isEqual; } console.log(checkEquality(obj1, obj2)) console.log(checkEquality(obj3, obj4))
Output
And the output in the console will be −
true false
- Related Articles
- Iterate through Object keys and manipulate the key values in JavaScript
- Fetching object keys using recursion in JavaScript
- Recursively list nested object keys JavaScript
- Changing value of nested object keys in JavaScript
- JavaScript: replacing object keys with an array
- Filter nested object by keys using JavaScript
- Update JavaScript object with another object, but only existing keys?
- How to convert square bracket object keys into nested object in JavaScript?
- Check if object contains all keys in JavaScript array
- Count number of entries in an object having specific values in multiple keys JavaScript
- How to get all the keys of a JSON object using GSON in Java?
- JavaScript map value to keys (reverse object mapping)
- The Keys and values method in Javascript
- Getting the keys in a SortedList object C#
- Maps in JavaScript takes keys and values array and maps the values to the corresponding keys

Advertisements