the answer is wrong for the question: "How do you compare two objects in JavaScript?"
See original GitHub issueThe code can’t work by calling
isDeepEqual(1, 2)
This is my answer:
function isDeepEqual(obj1, obj2, testPrototypes = false) {
if (obj1 === obj2) {
return true;
}
if (type(obj1) === 'Function' && type(obj2) === 'Function') {
return obj.toString() === obj2.toString();
}
if (type(obj1) === 'Date' && type(obj2) === 'Date') {
return obj1.getTime() === obj2.getTime();
}
if (type(obj1) !== type(obj2) || type(obj1) !== 'object') {
return false;
}
const prototypesAreEqual = testPrototypes
? isDeepEqual(
Object.getPrototypeOf(obj1),
Object.getPrototypeOf(obj2),
true
)
: true;
const obj1Props = Object.getOwnPropertyNames(obj1);
const obj2Props = Object.getOwnPropertyNames(obj2);
return (
obj1Props.length === obj2Props.length &&
prototypesAreEqual &&
obj1Props.every(prop => isDeepEqual(obj1[prop], obj2[prop]))
)
}
function type(obj) {
return Object.prototype.toString.call(obj);
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (8 by maintainers)
Top Results From Across the Web
How to determine equality for two JavaScript objects?
The simple answer is: No, there is no generic means to determine that an object is equal to another in the sense you...
Read more >How to compare two objects in JavaScript - Educative.io
To fix this, one option is to stringify both objects and then use the equality operators. · If the order of the properties...
Read more >Equality comparisons and sameness - JavaScript | MDN
Strict equality compares two values for equality. Neither value is implicitly converted to some other value before being compared. If the values ...
Read more >5 Different Ways to Deep Compare JavaScript Objects
This article showcases five ways to deep compare JS objects. ... solution to compare two objects, it has no problem comparing two strings....
Read more >JavaScript: Are TWO OBJECTS Equal [3 Solutions] - YouTube
JavaScript Interview Question : Are two objects equal?In this video, we will simulate an interview where we will write 3 possible solutions ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
right never reported test results did I… yeah it’s working in all cases. Never found an edge case that could be considered a problem except for a SO if I went deep with auto generating object references
The one you’ve submitted in the PR seems to be working. I want to do some more in depth tests when I have some time or if you’d like to submit some tests for it in the PR as a comment on the thread I’d be more than happy to look it over. Either way, this is a helpful find!