Home > Article > Web Front-end > What does the javascript every() method do?
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("不全为偶数"); }Example 2The following example checks whether the value of the element in the array is within the specified range. Range values are set via an object. This example demonstrates the use of the thisArg parameter.
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("部分不在范围内。"); }[Recommended learning:
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!