Check if all elements satisfy a condition

前端 未结 8 858
栀梦
栀梦 2020-12-10 11:05

I need a jQuery filter/map/each type function to check if ALL elements satisfy a condition:

function areAllValid(inputs){
     return $.someFunction(inputs,          


        
8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 11:18

    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);
    

提交回复
热议问题