Array.filter() method iterates through the array & calls a callback function on each element of the array and creates a new array with those elements only which have passed the test implemented by the callback function.
Syntax:
Array.filter(callback(element, index, array), thisArguments)
The callback function accepts three parameters :
- element — This parameter holds the value of array element being processed.
- index — This parameter holds the index of array element being processed. It is optional.
- array — This parameter holds the array itself. It is also optional.
- thisArguments — This parameter holds the context to passed to the callback function.
Example 1 :
In this example, we will filter out those elements for which the length of the string is greater than 3.
const words = ['React', 'javascript', 'angular', 'jquery', 'js']; const result = words.filter(getWord); function getWord() { return word.length > 5; } console.log(result);
Output :
["javascript", "angular", "jquery"]
Subscribe to our YouTube channel for more videos :