The filter()
method is used to iterate over each element of an array and filter among them with conditions.
array.filter()
const numbers = [1,2,3,4,5,6,7,8,9];
const evenNumbers = number.filter((element) => {
if(element%2 === 0) {
return element;
}
});
console.log(evenNumbers)
/* Prints
[ 2, 4, 6, 8 ]
*/
We can see that the filter()
loops over each element of the provided array, and applies conditions to return the required value.