Use of filter() array filter in JS

Use of filter() array filter in JS

1. Introduction

Array filter is a common method of front-end data processing. For the front-end, we need to process the data returned by the back-end before we can get the data we want and perform further operations. Sometimes the value returned to us by the backend can only be used as a reference.

2. Introduction to the Method

The filter() method creates a new array. The data in the array is the data that meets the conditions after filtering out the specified data.

Two major features of filter()

1.filter() does not detect empty arrays

2.filter() does not change the original array

Usage of filter() method:

array.filter(function(currentValue,index,arr), thisValue)
//currentValue: the value of the current element //index: the subscript of the current element //arr: the original array

3. Usage Examples

1. Get the elements in the array that meet the conditions

const school = [
  {
    occupation:"teacher",
    age:40
  },
  {
    occupation:"student",
    age:23
  },{
    occupation:"Programmer",
    age:1
  }
]
var newShool = school.filter(item => item.age > 20)
console.log(newShool);//[ { occupation: 'Teacher', age: 40 }, { occupation: 'Student', age: 23 } ]

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of the differences between js array find, some, filter, and reduce
  • jsp filter filter function and simple usage examples
  • How to filter data using orderBy and filter in angularJs
  • How to implement the filter encoding format in JSP development
  • Angularjs filter--filter and ng-repeat work together to create a miraculous effect

<<:  How to modify the root password of mysql in docker

>>:  Various transformation effects of HTML web page switching

Recommend

Detailed explanation of samba folder sharing server configuration under centos

1. Introduction Recently I found that there are m...

Implementation of CSS circular hollowing (coupon background image)

This article mainly introduces CSS circular hollo...

MySQL optimization strategy (recommended)

In summary: 1. Consider performance when designin...

Docker deployment and installation steps for Jenkins

First, we need a server with Docker installed. (I...

Detailed explanation of how Angular handles unexpected exception errors

Written in front No matter how well the code is w...

What you need to know about creating MySQL indexes

Table of contents Preface: 1. Create index method...

JavaScript Array Detailed Summary

Table of contents 1. Array Induction 1. Split a s...

SQL implementation LeetCode (185. Top three highest salaries in the department)

[LeetCode] 185. Department Top Three Salaries The...

A brief introduction to mysql mycat middleware

1. What is mycat A completely open source large d...

HTML+CSS+jQuery imitates the search hot list tab effect with screenshots

Copy code The code is as follows: <!DOCTYPE ht...

Use a table to adjust the format of the form controls to make them look better

Because I want to write a web page myself, I am al...

Brief analysis of the MySQL character set causing database recovery errors

Importing data with incorrect MySQL character set...

Learn about TypeScript data types in one article

Table of contents Basic Types any type Arrays Tup...

Introduction to HTML DOM_PowerNode Java Academy

What is DOM? With JavaScript, you can reconstruct...

What you need to know about MySQL auto-increment ID

Introduction: When using MySQL to create a table,...