Skip to content

Instantly share code, notes, and snippets.

@YannickLeRoux
Created April 8, 2019 19:01
Show Gist options
  • Save YannickLeRoux/0aa9a5374a48d50f18a75bb13c9232ae to your computer and use it in GitHub Desktop.
Save YannickLeRoux/0aa9a5374a48d50f18a75bb13c9232ae to your computer and use it in GitHub Desktop.
Check the equality of 2 arrays or that both contain same elements
// compares 2 arrays
const arraysEqual = (a, b) => {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length !== b.length) return false;
// compare that both contains same elements no matter the order (optional)
const aSort = a.sort();
const bSort = b.sort();
for (let i = 0; i < a.length; ++i) {
if (aSort[i] !== bSort[i]) return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment