Last active
March 26, 2021 22:57
-
-
Save smitroshin/94c696356d504605c6f92ab407f2a13c to your computer and use it in GitHub Desktop.
arrIsEqual
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checking equality between two arrays ignoring order. | |
* | |
* Source: https://www.30secondsofcode.org/blog/s/javascript-array-comparison | |
* | |
* Limitations: | |
* - primitive values only | |
* - first level only | |
* - can't compare objects | |
* | |
* @param {Array} a | |
* @param {Array} b | |
* @returns {Boolean} | |
*/ | |
const arrIsEqual = (a, b) => | |
a.length !== b.length | |
? false | |
: [...new Set([...a, ...b])].every( | |
(itm) => | |
a.filter((e) => e === itm).length === | |
b.filter((e) => e === itm).length, | |
); | |
export default arrIsEqual; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment