tags:

views:

53

answers:

1

I am using the $.grep() function http://docs.jquery.com/Utilities/jQuery.grep

$.each($.grep( [0,1,2], function(n,i){
  return n > 1;
}),
  function(){    alert(items matched); // do something   }
);

How to alert the no. of items that were matched in the alert box? Also how to avoid checking the count on each loop?

+1  A: 

Break it into two steps:

var arr = $.grep( [0,1,2], function(n,i){
  return n > 1;
});

if (arr.length) {
    alert("There are some items left after grep!");
    $.each(arr, function(){
        alert(arr.length); // do something
    });
} else {
    alert("No items left after grep!");
}
nickf
Yes that works..however if I change the condition to n > 4 and then try, the alert does not fire..any suggestions?
KJai
take a look at the edit now
nickf