In: Computer Science
Written In JavaScript
Deep comparison : Does Not Need To Be Recursive
The == operator compares objects by identity. But sometimes you’d prefer to compare the values of their actual properties.
Write a function deepEqual that takes two values and returns true only if they are the same value or are objects with the same properties, where the values of the properties are equal when compared with a recursive call to deepEqual.
To find out whether values should be compared directly (use the === operator for that) or have their properties compared, you can use the typeof operator. If it produces "object" for both values, you should do a deep comparison. But you have to take one silly exception into account: because of a historical accident, typeof null also produces "object".
The Object.keys function will be useful when you need to go over the properties of objects to compare them.
// Your code here. let obj = {here: {is: "an"}, object: 2}; console.log(deepEqual(obj, obj)); // → true console.log(deepEqual(obj, {here: 1, object: 2})); // → false console.log(deepEqual(obj, {here: {is: "an"}, object: 2})); // → true
Your test for whether you are dealing with a real object will look something like typeof x == "object" && x != null. Be careful to compare properties only when both arguments are objects. In all other cases you can just immediately return the result of applying ===.
Use Object.keys to go over the properties. You need to test whether both objects have the same set of property names and whether those properties have identical values. One way to do that is to ensure that both objects have the same number of properties (the lengths of the property lists are the same). And then, when looping over one of the object’s properties to compare them, always first make sure the other actually has a property by that name. If they have the same number of properties and all properties in one also exist in the other, they have the same set of property names.
Returning the correct value from the function is best done by immediately returning false when a mismatch is found and returning true at the end of the function.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
function deepEqual(obj1, obj2) {
// first check whether the type of obj1 and obj2 are same. If not, return false.
if(obj1 != null && obj2 != null && typeof obj1 == "object" && typeof obj2 == "object") {
let obj1Length = Object.keys(obj1).length;
let obj2Length = Object.keys(obj2).length;
// then, compare the length of obj1 and obj2 properties. If they are not equal, return false.
if(obj1Length === obj2Length) {
let isEqual = true;
// Then, loop to go through each property in obj1
for(var prop in obj1) {
// if the property exist in obj2, then go through each of the property and compare their values
if(obj2[prop] != null) {
// isEqual will store the result of deepEqual of every properties in obj1 and obj2
isEqual = isEqual && deepEqual(obj1[prop], obj2[prop]);
} else {
// the property of obj1 does not exist in obj2, return false.
return false;
}
}
// return isEqual
return isEqual;
} else {
// the length og obj1 and obj2 properties are not same, return false.
return false;
}
} else {
// if obj1 and obj2 are not properties, then do === to compare them.
return obj1 === obj2;
}
// return true
return true;
}
var obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));
// ? true
console.log(deepEqual(obj, {here: 1, object: 2}));
// ? false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// -> true