Skip to content

Instantly share code, notes, and snippets.

@loic-moriame
Created August 20, 2013 12:32
Show Gist options
  • Save loic-moriame/6280776 to your computer and use it in GitHub Desktop.
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
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