Reputation: 3630
I have some code in jQuery where I loop over an array and compare html values. When the values match, I put a boolean to true, and make sure it stays true.
var inArray = false;
$.each($myArray, function (index, value) {
inArray = inArray || value.html() === $draggable.html(); // Once it's true, it'll stay true
});
Is there a nice functional way of writing this without having to use the variable?
Upvotes: 1
Views: 54
Reputation: 3077
Use grep:-
$.grep($myArray, function(n) {
return n.html() === $draggable.html();
}).length > 0;
Upvotes: 2
Reputation: 104775
Break out of the loop once a true
value is hit:
$.each($myArray, function (index, value) {
inArray = inArray || value.html() === $draggable.html(); // Once it's true, it'll stay true
return !inArray; //returning true goes to next iteration, returning false breaks out.
});
Upvotes: 1