Apparently, it exists: Array.prototype.every. Example from mdn:
function isBigEnough(element, index, array) {
return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
passed = [12, 54, 18, 130, 44].every(isBigEnough);
This means that you do not have to write it manually. However, this function does not work on IE8-.
However, if you want to use a function that also works for IE8, you can use the manual implementation,
Or the polyfill shown on the mdn page .
Guide:
function all(array, condition){
for(var i = 0; i < array.length; i++){
if(!condition(array[i])){
return false;
}
}
return true;
}
Using:
all([1, 2, 3, 4], function(e){return e < 3})
all([1, 2, 3, 4], function(e){return e > 0})
source
share