In JavaScript, the every() method is used to check whether all elements of the array meet the specified conditions (provided through the callback function), the syntax is "array.every(function(currentValue,index,arr), thisValue) ".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Every() method is used to detect whether all elements of the array meet the specified conditions (provided through the callback function).
every() method uses the specified function to detect all elements in the array:
If it is detected that one element in the array is not satisfied, the entire expression returns false, And the remaining elements will not be tested again.
Returns true if all elements meet the condition.
Syntax:
array.every(function(currentValue,index,arr), thisValue)
Parameters:
##function(currentValue, index,arr): A callback (callbackfn) function, which cannot be omitted, can accept up to three parameters:
thisValue: Optional. The object is used as the execution callback, passed to the function, and used as the value of "this". If thisValue is omitted, the value of "this" is "undefined" .
function f (value, index, ar) { if (value % 2 == 0) { return true; }else { return false; } } var a = [2,4,5,6,8]; if (a.every(f)) { console.log("都是偶数"); }else{ console.log("不全为偶数"); }
var f = function (value) { if (typrof value !== 'number') { return false; }else { return value >=this.min && value <= this.max; } var a = [10,15,19]; var obj = {min : 10, max : 20}; if (a.every(f, obj)) { console.log("都在指定范围内。"); } else { console.log("部分不在范围内。"); }
javascript advanced tutorial]
The above is the detailed content of What does the javascript every() method do?. For more information, please follow other related articles on the PHP Chinese website!