JavaScript filter() function

detailedfilter()Functions in JavaScript

filter()It is a very important method for arrays.

This example iterates the arrayaAnd use the elements to construct a new arrayaReturn true when running the functionf()In eachaelement

const b = a.filter(f)

When you want to delete an item from the array, using filter() is a good example:

const items = ['a', 'b', 'c', 'd', 'e', 'f']
const valueToRemove = 'c'
const filteredItems = items.filter(item => item !== valueToRemove)
// ["a", "b", "d", "e", "f"]

This is how you can delete multiple items at the same time:

const items = ['a', 'b', 'c', 'd', 'e', 'f']
const valuesToRemove = ['c', 'd']
const filteredItems = items.filter(item => !valuesToRemove.includes(item))
// ["a", "b", "e", "f"]

Download mine for freeJavaScript beginner's manual


More js tutorials: