Home >Web Front-end >JS Tutorial >What does js filter mean?

What does js filter mean?

(*-*)浩
(*-*)浩Original
2019-05-20 20:14:238134browse



What does js filter mean?

The filter() method tests all elements using the specified function and creates a new one containing all elements that pass the test array.

Basic syntax of filter():

arr.filter(callback[, thisArg])

filter() parameter introduction:

Parameter name
Description
callback

Used Function that tests each element of an array. When calling, use the parameters (element, index, array) to return true to indicate that the element is retained (passed the test), and false to not retain it.

thisArg
Optional. The value used for this when callback is executed.

filter() usage instructions:

filter calls the callback function once for each element in the array, and Create a new array with all elements for which callback returns true or a value equivalent to true.
callback will only be called on indexes that have been assigned values, and will not be called on indexes that have been deleted or have never been assigned values. Elements that fail the callback test will be skipped and will not be included in the new array.

When callback is called, three parameters are passed in:
The value of the element
The index of the element
The array being traversed

If a thisArg parameter is provided to filter, it will be used as the this value when the callback is called. Otherwise, the this value of callback will be the global object in non-strict mode and undefined in strict mode.
filter will not change the original array.
filter The range of elements traversed has been determined before callback is called for the first time. Elements added to the array after calling filter will not be traversed by filter.
If existing elements are changed, the value they pass into callback is the value at the moment when filter traverses them. Elements that have been deleted or have never been assigned a value will not be traversed.

Filter() example: filter out all small values

The following example uses filter to create a new array, the elements of which are composed of the original array It consists of elements whose median value is greater than 10.

function isBigEnough(element) {
    return element >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
console.log(filtered);//[ 12, 130, 44 ]

Related learning recommendations: js video tutorial

The above is the detailed content of What does js filter mean?. 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
Previous article:What is package.jsonNext article:What is package.json