The Use of filter of Array Filter in JS

  • 2021-12-04 09:31:07
  • OfStack

Catalogue 1. Preface 2. Method introduction 3. Usage case summary

1. Preface

Array filter is a common method of front-end data processing. For front-end, we need to process the data returned by back-end before we can get the data we want, and carry out the next step. Sometimes the backend returns our values to me for reference only.

2. Brief introduction of the method

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

Two characteristics of filter ()

1. filter () does not detect empty arrays

2. filter () does not change the original array

Usage of the filter () method:


array.filter(function(currentValue,index,arr), thisValue)
//currentValue Value of the current element 
//index The subscript of the current element 
//arr Original array 

3. Usage cases

1. Get the eligible elements in the array


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

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: