Skip to content

Instantly share code, notes, and snippets.

@smitroshin
Last active March 26, 2021 22:57
Show Gist options
  • Save smitroshin/94c696356d504605c6f92ab407f2a13c to your computer and use it in GitHub Desktop.
Save smitroshin/94c696356d504605c6f92ab407f2a13c to your computer and use it in GitHub Desktop.
arrIsEqual
/**
* 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