Home > Web Front-end > JS Tutorial > body text

How to use filter in JavaScript

不言
Release: 2018-12-24 17:11:56
Original
4548 people have browsed it

Filter is a method of narrowing the contents of an array under specific conditions. It is used to judge a single element under the conditions specified by the callback function and only retrieve elements that match the conditions. Therefore, let’s take a closer look in this article. See how to use the filter filter in How to use filter in How to use filter in How to use filter in JavaScript.

How to use filter in How to use filter in How to use filter in JavaScript

Let’s first take a look at the basic syntax of filter

When using filter, please specify the filter method of the array.

array.filter(callback [,that]);
Copy after login

For array, you need to specify a pre-created array object.

For callbacks, you can specify the value "value" of the array element, the numeric index "index" of the array element, and the array object "arrayObj" that stores the array element.

For each array element, the elements for which callbak returns true will be generated as a new array, and the elements for which callcak does not return true will be skipped and not included in the new array.

Let’s look at specific examples

The following is an example of extracting specific conditions from an array by actually using the filter method

Extract odd numbers from the array

The code is as follows
##

var data = [1, 4, 7, 12, 21];
var result = data.filter(function(value) {
  return value % 2 === 1;
});
console.log(result);
Copy after login

The running results are as follows


How to use filter in How to use filter in How to use filter in JavaScript

Delete numbers less than 5 from the array

The code is as follows

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
function isMinNum(value) {
  return (value >= 5);
}
var filterNum = numbers.filter(isMinNum);
console.log(filterNum);
Copy after login

The running result is as follows

How to use filter in How to use filter in How to use filter in JavaScript

Extract the string that matches the condition from the string

The code is as follows

var items = ["item1", "item2", "item3"];
var filterItems = items.filter(function(value) {
  return value === "item2";
});
console.log(filterItems);
Copy after login
The running result is as follows

How to use filter in JavaScript

This article ends here. For more exciting content, you can pay attention to the relevant tutorial columns of the php Chinese website! ! !

The above is the detailed content of How to use filter in JavaScript. 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