Home  >  Article  >  Web Front-end  >  What does the javascript every() method do?

What does the javascript every() method do?

青灯夜游
青灯夜游Original
2021-11-04 17:20:183440browse

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) ".

What does the javascript every() method do?

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:

  • value: the value of the current array element, which cannot be omitted.

  • index: The numeric index of the current array element.

  • array: The array object to which the current element belongs.

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" .

Every() method will call the callbackfn function once for each array element in ascending order until the callbackfn function returns false. If an element is found that causes callbackfn to return false, the every() method immediately returns false; otherwise, the every() method returns true. The every() method does not call this callback function for missing elements in the array.

In addition to array objects, the every() method can be used by any object with a length property that has its own numerically indexed property name, such as associative array objects, Arguments, etc.

Example 1

The following example checks whether the elements in the array are all even numbers and prompts.

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 2

The 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn