Home Web Front-end JS Tutorial How to use filter function

How to use filter function

Feb 21, 2019 pm 04:08 PM
filter

The arr.filter() function in How to use filter function is used to create a new array from a given array that contains only those elements in the given array that meet the conditions set by the parameter function. Let's take a closer look at how to use filter().

How to use filter function

The basic syntax of filter() is as follows:

var newArray = arr.filter(arg_function[, this_arg])
Copy after login

The parameter of the filter() function is another function that defines each element of the array. Check the conditions. This arg_function itself has three parameters:

array: This is the array in which the .filter() function is called.

index: This is the index of the current element processed by the function.

element: This is the current element that the function is processing.

Another parameter this_arg is used to indicate that the function uses this value when executing the parameter function.

Let’s look at a specific example

The function filter() creates a new array that only contains elements that meet the conditions checked by the isPositive() function.

Example 1:

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>

<script> 
function isPositive(value) { 
  return value > 0; 
} 
  
function func() { 
    var filtered = [112, 52, 0, -1, 944].filter(isPositive); 
    document.write(filtered); 
} 
func(); 
</script> 
</body>
</html>
Copy after login

The output result is: 112,52,944

Example 2:

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
<script> 
function isEven(value) { 
  return value%2 == 0; 
} 
  
function func() { 
    var filtered = [11, 98, 31, 23, 944].filter(isEven); 
    document.write(filtered); 
} 
func(); 
</script> 
</body>
</html>
Copy after login

The output result is: 98,944

This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the '[Vue warn]: Failed to resolve filter' error How to solve the '[Vue warn]: Failed to resolve filter' error Aug 19, 2023 pm 03:33 PM

Methods to solve the "[Vuewarn]:Failedtoresolvefilter" error During the development process using Vue, we sometimes encounter an error message: "[Vuewarn]:Failedtoresolvefilter". This error message usually occurs when we use an undefined filter in the template. This article explains how to resolve this error and gives corresponding code examples. When we are in Vue

What is the principle and registration method of filter in Springboot What is the principle and registration method of filter in Springboot May 11, 2023 pm 08:28 PM

1. Filter First look at the location of the filter of the web server. Filter is a chain connected before and after. After the previous processing is completed, it is passed to the next filter for processing. 1.1Filter interface definition publicinterfaceFilter{//Initialization method, only executed once in the entire life cycle. //Filtering services cannot be provided until the init method is successfully executed (failure such as throwing an exception, etc.). //The parameter FilterConfig is used to obtain the initialization parameter publicvoidinit(FilterConfigfilterConfig)throwsServletException;//

How to integrate Filter in SpringBoot2 How to integrate Filter in SpringBoot2 May 16, 2023 pm 02:46 PM

First define a Filter for unified access URL interception. The code is as follows: publicclassUrlFilterimplementsFilter{privateLoggerlog=LoggerFactory.getLogger(UrlFilter.class);@OverridepublicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{H

How to filter in java How to filter in java Apr 18, 2023 pm 11:04 PM

Note 1. If the Lambda parameter generates a true value, the filter (Lambda that can generate a boolean result) will generate an element; 2. When false is generated, this element will no longer be used. Example to create a List collection: ListstringCollection=newArrayList();stringCollection.add("ddd2");stringCollection.add("aaa2");stringCollection.add("bbb1");stringC

CSS visual property analysis: box-shadow, text-shadow and filter CSS visual property analysis: box-shadow, text-shadow and filter Oct 20, 2023 pm 12:51 PM

Analysis of CSS visual properties: box-shadow, text-shadow and filter Introduction: In web design and development, CSS can be used to add various visual effects to elements. This article will focus on the three important properties of box-shadow, text-shadow and filter in CSS, including their usage and effect display. Below we analyze these three properties in detail. 1. box-shadow (box shadow) box-shado

Detailed explanation of CSS blur properties: filter and backdrop-filter Detailed explanation of CSS blur properties: filter and backdrop-filter Oct 20, 2023 pm 04:48 PM

Detailed explanation of CSS fuzzy attributes: filter and background-filter Introduction: When designing web pages, we often need some special effects to increase the visual appeal of the page. The blur effect is one of the common special effects. CSS provides two blur attributes: filter and background-filter, which are used to blur element content and background content respectively. This article explains these two properties in detail and provides some concrete code examples. 1. filter

Optional class in Java 8: How to filter possibly null values ​​using filter() method Optional class in Java 8: How to filter possibly null values ​​using filter() method Aug 01, 2023 pm 05:27 PM

Optional class in Java8: How to use the filter() method to filter possibly null values ​​In Java8, the Optional class is a very useful tool that allows us to better handle possibly null values ​​and avoid the occurrence of NullPointerException. The Optional class provides many methods to manipulate potential null values, one of the important methods is filter(). The function of the filter() method is that if Option

How to use filters to format and process data in Vue How to use filters to format and process data in Vue Oct 15, 2023 pm 03:50 PM

Use filters to format and process data in Vue In Vue, we can format and process data by using filters. Filter is a function that can be called directly in the template. It can process the data to be displayed and return the processed results. In this article, we will introduce how to use filters to format and process data, and provide specific code examples. Register filter In the Vue instance, we need to register a filter first so that it can be used in the model

See all articles