Created
December 6, 2013 20:43
-
-
Save maxbeatty/7831767 to your computer and use it in GitHub Desktop.
Compare two arrays in CoffeeScript
This file contains hidden or 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
# 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