.filter() (JavaScript)
.filter() creates a new array containing only the elements that pass a test you provide. The original array is left unchanged.
JavaScript
You give .filter() a function that returns true or false for each element. Elements that return true are kept; the rest are dropped.
It is the standard way to narrow a list down to the items you care about.
Examples
JavaScript
const nums = [1, 2, 3, 4, 5];
const evens = nums.filter(n => n % 2 === 0);
console.log(evens);Output:
[2, 4]