Skip to content

Instantly share code, notes, and snippets.

@LB-Digital
Created October 30, 2018 03:30
Show Gist options
  • Save LB-Digital/ef9d7a82903103146e49755a00108d26 to your computer and use it in GitHub Desktop.
Save LB-Digital/ef9d7a82903103146e49755a00108d26 to your computer and use it in GitHub Desktop.
An array method to check if one array is equal to another. NOTE: this is only built for simple arrays with singular values, e.g: ['a','b','c'], not complex arrays, e.g: ['a', {this:'that',up:'down'}]
Array.prototype.isEqualTo = function(arr2){ // defining the Array method
var arr1 = this; // accesing the first array
if (arr1.length==arr2.length){ // if they have the same length
arr1 = arr1.filter(item=>{ // filter through arr1
if (arr2.indexOf(item) > -1){ // if item from arr1 is in arr2
return false; // remove from arr1 so we know its been accounted for
}
return true; // keep in arr1 as not in arr2
});
if (arr1.length == 0) return true; // no items left so must be equal
return false; // NOT EQUAL
}else{ // different lengths so can't be equal
return false; // NOT EQUAL
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment