Search code examples
jqueryprototypejs

What is the equivalent of prototype .any in Jquery?


We have one function called .any in Prototype. I want the same like in Jquery.

My Prototype code is:

 if (item_name == '' || $R(1,ind).any(function(i){return($F("bill_details_"+i+"_narration") == item_name)})) {
     alert("This item already added.");
 }

I want to perform the Equivalent function using Jquery.

Please help me to achieve the desired output. Thanks in Advance..


Solution

  • For IE 9+ it's built in:

    The some() method tests whether some element in the array passes the test implemented by the provided function.

    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some

    [2, 4, 6, 8, 10].some(function(n) { return n > 5; });
    // -> true (the iterator will return true on 6)
    

    For IE 8 and below:

    Prototype any

    [2, 4, 6, 8, 10].any(function(n) { return n > 5; });
    // -> true (the iterator will return true on 6)
    

    You can use jQuery.grep:

    jQuery.grep([2, 4, 6, 8, 10], function(n) { return n > 5; }).length > 0;
    // -> true (as grep returns [6, 8, 10])
    

    Underscore _.any or _.some

    _.any([2, 4, 6, 8, 10], function(n) { return n > 5; });
    // -> true (the iterator will return true on 6)