Mootools doesn't have a built in method to do this but it's easy enough to write your own!
Be advised that for very large arrays this might be quite slow. In JavaScript, unless array1 is
*the same array* (rather than just an array containing the same information) as array2 it will be 'different' so:
is always false, whereas
will be true. If we're talking about a very large list of user ids, then we're going to have to run through every entry in the arrays and compare them!
We can add a method to the Array prototype thus:
- Array.implement({
- equalTo: function(arr){
- if(this.length !== arr.length){
- return false;
- }
- for(var i = this.length - 1; i >= 0; i--){
- if(this[i] !== arr[i]){
- return false;
- }
- }
- return true;
- }
- });
this first establishes that the arrays are the same length (it's a very quick false if they aren't!) and then runs backwards through the arrays checking each entry. I decided to make it go backwards because I figure you (and anyone else who uses this) are more likely to add something to the end than the begining so it might find the change more quickly? Just invert the for loop if you don't agree!
It can be used thus:
- [1, 2, 3].equalTo([1, 2, 4, 3]) // false
- [1, 2, 3].equalTo([1, 2, 3]) // true
- [].equalTo([]) // true
or with appropriate variable, more generally:
- a.equalTo(b) // for a and b arrays
Hope that helps, good luck