[jQuery] Re: compare two arrays
This can be done using mapreduce or other functions, but they are only supported in Firefox atm. Writing a double for loop is easy to code and wider supported. ~Sean On 8/28/07, Potluri [EMAIL PROTECTED] wrote: 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. Straight answers are appreciated. Regards, Vijay Potluri -- View this message in context: http://www.nabble.com/compare-two-arrays-tf4344387s15494.html#a12376633 Sent from the JQuery mailing list archive at Nabble.com.
[jQuery] Re: compare two arrays
Map and reduce iterate over the array as well so the only speedup you'd gain from using them is the fact that they're natively implemented and not written in JavaScript themselves. They still represent functions that loop over arrays. On Aug 29, 7:12 am, Sean Catchpole [EMAIL PROTECTED] wrote: This can be done using mapreduce or other functions, but they are only supported in Firefox atm. Writing a double for loop is easy to code and wider supported. ~Sean On 8/28/07, Potluri [EMAIL PROTECTED] wrote: 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. Straight answers are appreciated. Regards, Vijay Potluri -- View this message in context: http://www.nabble.com/compare-two-arrays-tf4344387s15494.html#a12376633 Sent from the JQuery mailing list archive at Nabble.com.
[jQuery] Re: compare two arrays
I know another easy way to do this with only one loop but not in jquery way. var arrayA=[1,2,3,4,5] var arrayB=[1,b,c,1,2] for(var i=0;iarrayA.length;i++) { if(arrayB.indexOf(arrayA[i]) != -1) { return true; } return false; } Michael Geary wrote: 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 -- View this message in context: http://www.nabble.com/compare-two-arrays-tf4344387s15494.html#a12387938 Sent from the JQuery mailing list archive at Nabble.com. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups jQuery (English) group. To post to this group, send email to jquery-en@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/jquery-en?hl=en -~--~~~~--~~--~--~---
[jQuery] Re: compare two arrays
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