Last active
December 20, 2018 12:08
-
-
Save msaxena25/d6ef47bcd3b1f08724728845255f4131 to your computer and use it in GitHub Desktop.
JavaScript - Compare two Objects are same or not Best & Simple way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function equal(obj1, obj2) { | |
let keys = Object.keys(obj1); // first find keys of first Object | |
let output = false; | |
for (let i = 0 ; i < keys.length ; i++) { | |
output = (obj2.hasOwnProperty(keys[i]) && obj1[keys[i]] == obj2[keys[i]]) ? true : false; | |
} | |
return output; | |
} | |
var o1 = {b : 1, c : 2}; | |
var o2 = {b: 1, c : 2}; | |
equal(o1, o2); --> true | |
var o1 = {b : 1, c : 2}; | |
var o2 = {b: 1, c : 2 , d: 4}; | |
equal(o1, o2); --> false | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment