Created
August 20, 2013 12:32
-
-
Save loic-moriame/6280776 to your computer and use it in GitHub Desktop.
Add function "array.isEqualTo(arrayComparision)" to compare 2 one-dimensional's arrays
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
Array.prototype.isEqualTo = function(array) { | |
if(!array) { | |
return false; | |
} | |
if(!(array instanceof Array)) { | |
return false; | |
} | |
if(this.length != array.length) { | |
return false; | |
} | |
var i = this.length; | |
while (i--) { | |
if (this[i] !== array[i]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
[].isEqualTo([]) //true | |
[1,2,3].isEqualTo([1,2,3,]) //true | |
[1,2,3].isEqualTo([1,2,3]) //true | |
['batman','robin'].isEqualTo(['batman','robin']) //true | |
[].isEqualTo() //false | |
[].isEqualTo(null) //false | |
[].isEqualTo(undefined) //false | |
[].isEqualTo('bonjour') //false | |
[1,2,3].isEqualTo([3,2,1]) //false | |
[1,2,3].isEqualTo(['1','2','3']) //false | |
[1,2,3].isEqualTo([1,2,3,4]) //false | |
['batman','robin'].isEqualTo(['robin','batman']) //false | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment