Skip to content

Instantly share code, notes, and snippets.

@jakewtaylor
Created January 25, 2019 14:47
Show Gist options
  • Save jakewtaylor/eb5ab06314b000e8d599d42be4e44b9c to your computer and use it in GitHub Desktop.
Save jakewtaylor/eb5ab06314b000e8d599d42be4e44b9c to your computer and use it in GitHub Desktop.
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