Skip to content

Instantly share code, notes, and snippets.

@pzi
Created August 3, 2020 06:16
Show Gist options
  • Save pzi/8c77c58f005718bbf01b55e9f9b6c121 to your computer and use it in GitHub Desktop.
Save pzi/8c77c58f005718bbf01b55e9f9b6c121 to your computer and use it in GitHub Desktop.
function isEqualArray(a, b) {
if (a === b) return true
if (a == null || b == null) return a !== a && b !== b
if (a.length !== b.length) return false
const aHasB = a.every(aValue => b.includes(aValue))
const bHasA = b.every(bValue => a.includes(bValue))
return aHasB && bHasA
}
isEqualArray([1], undefined) // false
isEqualArray(undefined, undefined) // true
isEqualArray(null, undefined) // false
isEqualArray(null, [1]) // false
isEqualArray(undefined, [1]) // false
isEqualArray([1], [1]) // true
isEqualArray([1], [1, 2]) // false
isEqualArray([1, 2], [1]) // false
isEqualArray([1, 2], [1, undefined]) // false
isEqualArray([1, undefined], [1, undefined]) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment