question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

the answer is wrong for the question: "How do you compare two objects in JavaScript?"

See original GitHub issue

The 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:closed
  • Created 5 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
skatcat31commented, Sep 9, 2018

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

1reaction
skatcat31commented, Sep 1, 2018

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found