Created
January 25, 2019 14:47
-
-
Save jakewtaylor/eb5ab06314b000e8d599d42be4e44b9c to your computer and use it in GitHub Desktop.
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
export const arrayEqual = (arr1, arr2) => { | |
// If either item is falsey we can short circuit and warn | |
if (!arr1 || !arr2) { | |
console.warn('arrayEqual(): One of the supplied arrays is falsey.'); | |
return false; | |
} | |
// If they're equal we can short circuit | |
if (arr1 === arr2) return true; | |
// If the lengths are different we can short circuit | |
if (arr1.length !== arr2.length) return false; | |
// Loop through every item in array 2 | |
for (let i = 0; i < arr2.length; i++) { | |
// If the first array doesn't contain the current | |
// item, the arrays are not equal. | |
if (!arr1.includes(arr2[i])) { | |
return false; | |
} | |
} | |
// We got this far - the arrays must be the same! | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment