Describing the filter
Operation
filter
is used for filtering an array based on a condition-
It accepts a callback function with the following arguments:
currentValue:
Current element processed in the arrayindex
: Optional index ofcurrentValue
array
: Optional array that is calledthisArg
: Optional value to use asthis
Defining an Array
let fruits = ["Apple", "Banana"];
console.log(fruits.length);
// 2
Using the filter
Operation
// Regular function
fruits.filter(function(item, index, array) {
return item.length > 5;
})
// Array["Banana"]
// Arrow function
fruits.filter(item => item.length > 5);
// Array["Banana"]
Previous
Next