filter()
method
ES5
Creates a new array with all elements that pass the test implemented by the provided function.
Syntax
array.filter(callback(element, index, array))
Example
javascript
const numbers = [1, 2, 3, 4, 5, 6];
const evens = numbers.filter(n => n % 2 === 0);
console.log(evens); // [2, 4, 6]
const products = [{name:'A', price:10}, {name:'B', price:50}, {name:'C', price:25}];
const affordable = products.filter(p => p.price < 30);
console.log(affordable); // [{name:'A',price:10}, {name:'C',price:25}]