-
Notifications
You must be signed in to change notification settings - Fork 519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compare two arrays regardless of order has siteeffects #475
Comments
Can you make a PR, @relaxdays-bewerbung ? |
I didn't understand very well why you suggested cloning the arguments @relaxdays-bewerbung, what is exactly the issue with using the current snippet ? |
The issue is that a and b are different after the comparison a and b are different. for example if you use this on → a and b both will be [1, 2, 3] afterwards. |
Oh, I understand you now very well ! |
Hi there,
your Sippet for comparing Arrays has an important downside. It sorts the original arrays, which is not the espected behaviour.
I suggest to fix it by cloning the arguments before sorting it or using Sets if number of sam items does not matter.
The current snippet:
//
a
andb
are arraysconst isEqual = (a, b) => JSON.stringify(a.sort()) === JSON.stringify(b.sort());
// Examples
isEqual([1, 2, 3], [1, 2, 3]); // true
isEqual([1, 2, 3], [1, 3, 2]); // true
isEqual([1, 2, 3], [1, '2', 3]); // false
The text was updated successfully, but these errors were encountered: