> From: Potluri > > Is there a jquery way to compare two arrays without looping. > > like I have an array1 [a,b,1,2,3] > and array2 [b,c,d,11 ]. > > Is there a way like array1.compare(array2). which returns > true if atleast one element among the 2 arrays matches.
Somebody has to loop, whether it's you or jQuery. :-) I don't know of anything built into jQuery, but if all the array elements are numbers or strings, it's easy to write a function that should be reasonably fast (certainly much faster for long arrays than the obvious nested loop solution): function arrayMatch( a, b ) { var o = {}; for( var i = a.length; --i >= 0; ) o[ a[i] ] = true; for( var i = b.length; --i >= 0; ) if( o[ b[i] ] ) return true; return false; } It may be possible to pick up some extra speed in the case where one array is much longer than the other, by choosing the shorter vs. longer array for each of the two steps. But it's not certain which would be faster - the short array in the first loop, or the short array in the second loop. You would have to benchmark it in a variety of browsers to know for sure. If the arrays may contain other kinds of objects (e.g. if a, b, etc. in your example are object references and not strings or string variables), then you'd have to go back to the nested loops instead of the above code. Now watch somebody make a fool of me and point to a jQuery function for this. :-) -Mike