What is the equivalent of prototype .any in Jquery?
Solution 1
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)
Solution 2
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.
Solution 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.");
}
![Can Can](https://i.stack.imgur.com/TgXPA.jpg?s=256&g=1)
Comments
-
Can Can about 2 years
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..
-
Can Can over 11 yearscan u post the equivalent jquery for my above prototype code. Please
-
Alnitak over 11 years
$F
is still using Prototype, not jQuery. -
Uri over 9 yearsAs for performance though, be wary that .any should stop looking after the first occurence, while .grep will traverse the entire array.
-
CMCDragonkai over 7 yearsI wonder if you can use something liek this stackoverflow.com/a/22268145/582917 to break out of the non-short circuiting versions of the aggregation functions.
-
Spongman about 7 yearsfor jquery, $.grep().length is not as efficient as $.is()