Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use filter () array filter in JS

Shulou Source: shulou.com Published: 2022-06-02 18:59:55 09月24日 Update

This article is about how to use the filter () array filter in JS. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

I. Preface

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 for further operation. Sometimes the value returned to us by the back end can only be used as a reference.

2. Brief introduction of the method

The filter () method creates a new array of eligible data filtered out of the specified data.

Two characteristics of filter ()

1.filter () does not detect an empty array

2.filter () does not change the original array

The usage of the 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 case

1. Get the eligible elements in the array

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}] Thank you for reading! This is the end of the article on "how to use the filter () array filter in JS". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Array data method filter element content front end student more condition article teacher reference processing good practical subscript can create preface commonly used Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno Shulou Technology Microsoft NVidia