Home  >  Article  >  Web Front-end  >  Is filter in js an es6 method?

Is filter in js an es6 method?

WBOY
WBOYOriginal
2022-08-18 17:19:20998browse

The filter of js is a method of es6; the filter method is a new array method of es6, which is used to filter array elements and return the elements in the array that meet the specified conditions. This method will also pass the array elements into a Callback function, in the callback function, it is judged whether the element meets the specified conditions. If the conditions are not met, it will be returned. The syntax is "arr.filter(callback function, thisValue)".

Is filter in js an es6 method?

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

Is filter in js a method of es6?

filter() is a new array method in es6, which is used to filter array elements and return elements in the array that meet specified conditions.

The filter() method will pass the array elements into a callback function. In the callback function, it will determine whether the element meets the specified conditions and return if it meets the specified conditions.

Syntax:

arr.filter(回调函数,thisValue)

Callback function: Each element in the array will execute this function, which is used to specify conditions and process elements

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"

Format of callback function:

function callbackfn(Value,index,array)

Accepts up to three parameters:

value: current array element The value cannot be omitted.

index: The numerical index of the current array element.

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

Return value: is a new array containing all values ​​for which the callback function returns true. If the callback function returns false for all elements of array, the length of the new array is 0.

The example is as follows:

var a = [31,33,35,37,39,41,43,45,57,49,51,53];var a1 = a.filter(function(value, index, ar) {
    high = Math.floor(Math.sqrt(value)) + 1;
    for (var div = 2; div <= high; div ++) {
        if (value % div == 0) {
            return false;
        }
}
    return true;
}
);console.log(a1);//31,37,41,43,53

Output result:

Is filter in js an es6 method?

[Related recommendations: javascript video tutorial, webfrontend

The above is the detailed content of Is filter in js an es6 method?. 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