- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the property of the difference between two objects in JavaScript
Let’s say, we are given two objects that have similar key value pairs with one or key having different values in both objects. Our job is to write a function that takes in the two objects as argument and returns the very first key it finds having different values. If all the keys have exact same values, it should return -1.
Here are the sample objects −
const obj1 = { name: 'Rahul Sharma', id: '12342fe4554ggf', isEmployed: true, age: 45, salary: 190000, job: 'Full Stack Developer', employedSince: 2005 } const obj2 = { name: 'Rahul Sharma', id: '12342fe4554ggf', isEmployed: true, age: 45, salary: 19000, job: 'Full Stack Developer', employedSince: 2005 }
We will take in the two objects, iterate over the first one using forEach() loop, checking for equality in both objects, if the values at any point don’t match we will update a flag, exit out of the loop and return the specific key. If we iterate through the whole loop, it means that everything matched, in that case we will return -1.
The full code for this will be −
Example
const obj1 = { name: 'Rahul Sharma', id: '12342fe4554ggf', isEmployed: true, age: 45, salary: 190000, job: 'Full Stack Developer', employedSince: 2005 } const obj2 = { name: 'Rahul Sharma', id: '12342fe4554ggf', isEmployed: true, age: 45, salary: 19000, job: 'Full Stack Developer', employedSince: 2005 } const difference = (obj1, obj2) => { let keyFound = false; Object.keys(obj1).forEach(key => { if(obj1[key] !== obj2[key]){ return keyFound = key; } }); return keyFound || -1; }; console.log(difference(obj1, obj2));
Output
The output in the console will be −
salary
- Related Articles
- How to get the difference between two arrays in JavaScript?
- How to write a JavaScript function to get the difference between two numbers?
- Finding the difference between two arrays - JavaScript
- Get the difference between two timestamps in seconds in MySQL?
- Get the total number of same objects in JavaScript
- How to get the difference between two dates in Android?
- C# Program to get the difference between two dates
- Find the Symmetric difference between two arrays - JavaScript
- How to get the number of seconds between two Dates in JavaScript?
- How to get the number of days between two Dates in JavaScript?
- Will the gravity work between any two objects If any thing come between those objects two objects?
- C# Program to get the difference between two dates in seconds
- Sort array according to the date property of the objects JavaScript
- How to calculate the difference between two dates in JavaScript?
- Not able to get the difference between two dates (SAP)
