Home > Web Front-end > JS Tutorial > How to use filter function to filter elements in an array?

How to use filter function to filter elements in an array?

王林
Release: 2023-11-18 09:00:50
Original
1049 people have browsed it

How to use filter function to filter elements in an array?

How to use the filter function to filter elements in an array?

Introduction:
In programming, we often need to filter elements with specific conditions from an array. In JavaScript, we can use the filter function to achieve this function. The filter function is a higher-order function that accepts a callback function as a parameter and returns qualified elements in the array as a new array.

Usage:
When using the filter function, we first need to define a callback function, which will be used to determine whether the elements in the array meet our filtering conditions. The callback function will receive three parameters: the array element, the index of the element, and the original array itself. Then, we can use this callback function as a parameter of the filter function and pass the array to be filtered into the filter function.

The following is a sample code that demonstrates how to use the filter function to filter elements greater than 10 in an array:

const numbers = [5, 15, 8, 12, 3, 20];

const filteredNumbers = numbers.filter(function(number) {
  return number > 10;
});

console.log(filteredNumbers); // [15, 12, 20]
Copy after login

In the above code, we first define an array numbers, which contains a set of number. Then, we use the filter function to filter this array. The condition in the callback function is whether the element is greater than 10, and the elements that meet the condition will be returned to the new array filteredNumbers. Finally, we use console.log to print out the filteredNumbers, and the result is [15, 12, 20].

In addition to using anonymous functions as callback functions, we can also use arrow functions to simplify the code:

const filteredNumbers = numbers.filter(number => number > 10);
Copy after login

The above code implementation has the same effect as the previous code, but the writing method is more concise.

Summary:
Using the filter function can easily filter elements in the array that meet the conditions. We can set the filtering conditions by defining a callback function, and then pass the array to be filtered into the filter function. Using the filter function can improve programming efficiency and make the code more concise.

The above is the detailed content of How to use filter function to filter elements in an array?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template