Skip to content

Instantly share code, notes, and snippets.

@maxbeatty
Created December 6, 2013 20:43
Show Gist options
  • Save maxbeatty/7831767 to your computer and use it in GitHub Desktop.
Save maxbeatty/7831767 to your computer and use it in GitHub Desktop.
Compare two arrays in CoffeeScript
# src: http://stackoverflow.com/a/14853974/613588
# attach the .compare method to Array's prototype to call it on any array
Array.prototype.compare = (array) ->
# if the other array is a falsy value, return
return false unless array
# compare lengths - can save a lot of time
return false if @length isnt array.length
for i in [0..@length]
# Check if we have nested arrays
if @[i] instanceof Array and array[i] instanceof Array
# recurse into the nested arrays
return false unless @[i].compare array[i]
else if @[i] isnt array[i]
# Warning - two different object instances will never be equal:
# {x:20} != {x:20}
return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment