15

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..

Alnitak
  • 334,560
  • 70
  • 407
  • 495
Can Can
  • 3,644
  • 5
  • 32
  • 56

5 Answers5

23

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)
jantimon
  • 36,840
  • 23
  • 122
  • 185
5

ES5 has a built-in function called Array.prototype.some which tests for whether any element in an array matches a predicate function, and which stops iterating as soon as a matching element is found.

.some(function(el) {
    return el.value === item_name;
});

Your problem then just becomes one of creating an array of the desired elements, which is harder than it would be in Prototype because there's no "range" operator in jQuery. Fortunately $.map iterates over empty elements, even though the built-in Array.prototype.map doesn't so you can use new Array(ind):

var found = $.map(new Array(ind), function(_, x) {
    return "bill_details_" + (x + 1) + "_narration";
}).some(function(id) {
    var el = document.getElementById(id);
    return el && (el.value === item_name);
});

The link above includes a shim for .some for older browsers.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
3

JQuery has .is() method, from documentation: Check the current matched set of elements against a selector, element, or jQuery object and return *true* if at least one of these elements matches the given arguments. Thus an equivalent code is:

 if (item_name == '' || $([1,ind]).is(function(i) { return $('#bill_details_'+i+'_narration').attr('name') == item_name; })) {
      alert("This item already added.");
 }
Igor Kustov
  • 3,228
  • 2
  • 34
  • 31
0

It seems http://api.jquery.com/jQuery.grep/ is what you are looking for.

try

if (item_name == '' || $.grep([1,ind],function(i){return($("#bill_details_"+i+"_narration").attr("name") == item_name)}).length>0) {
     alert("This item already added.");
 }
Christophe Debove
  • 6,088
  • 20
  • 73
  • 124
0

According to JQuery's documentation if you return a false in the callback function it will break the loop:

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

Taken from: http://api.jquery.com/jquery.each/

surbina
  • 1,094
  • 8
  • 5