You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 and b are arrays
const isEqual = (a, b) => JSON.stringify(a.sort()) === JSON.stringify(b.sort());
Activity
phuocng commentedon Sep 30, 2021
Can you make a PR, @relaxdays-bewerbung ?
elkarouani commentedon Dec 20, 2021
I didn't understand very well why you suggested cloning the arguments @relaxdays-bewerbung, what is exactly the issue with using the current snippet ?
relaxdays-bewerbung commentedon Dec 22, 2021
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 = [1, 3, 2];
b = [3, 2, 1];
isEqual(a, b);
→ a and b both will be [1, 2, 3] afterwards.
a and be change during the function.
elkarouani commentedon Dec 22, 2021
Oh, I understand you now very well !