I need a jQuery filter/map/each type function to check if ALL elements satisfy a condition:
function areAllValid(inputs){ return $.someFunction(inputs, function(input) { return input.length > 0; }); }
If ALL inputs have length > 0 someFunction
should return true. Anything like this in jQuery?
Method #1 : Using all() We can use all() , to perform this particular task. In this, we feed the condition and the validation with all the elements is checked by all() internally.
Using Count() The python list method count() returns count of how many times an element occurs in list. So if we have the same element repeated in the list then the length of the list using len() will be same as the number of times the element is present in the list using the count(). The below program uses this logic.
To check if the list contains an element in Python, use the “in” operator. The “in” operator checks if the list contains a specific item or not. It can also check if the element exists on the list or not using the list.
Python will lazily evaluate the boolean conditions left to right in an if statement.
The answer is YES, it has a method grep that can meet your requirement. For example:
inputs= jQuery.grep(inputs, function(input){ return input.length>0; }); if(inputs.length>0) return true; return false;
I don't test it, maybe it has tiny issue but should be almost like this.
You don't need to use jQuery to do this. You can do this in native JavaScript using Array.prototype.every, which is supported in IE 9+.
function areAllValid(inputs){ return inputs.every(function(input) { return input.length > 0; }); }
If you are using EcmaScript 2015, you can tidy it up further:
var areAllValid = inputs => inputs.every(input => input.length > 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With