filter()
.filter() creates a new array that keeps only the elements that pass a test.
Use it when you want to remove items from an array without changing the original array.
Example
const numbers = [1, 2, 3, 4, 5, 6, 7]
const evenNumbers = numbers.filter(isEven)
function isEven(element) {
return element % 2 === 0
}
console.log(evenNumbers) // [2, 4, 6]Another Example
const ages = [16, 17, 18, 19, 20, 60]
const adults = ages.filter((age) => age >= 18)
console.log(adults) // [18, 19, 20, 60]