JS array.filter()

JavaScript has many array methods that can help you solve a variety of problems. One of those methods is the filter() method. The filter() method creates a new array with all of the elements that pass the test implemented by the provided function.

Syntax

array.filter(function(currentValue, index, array), thisValue)

Parameters

A function to be run for each element in the array.
function(currentValue, index, array)

Function arguments:

currentValue

  • The value of the current element
  • Required

Index

  • The array index of the current element
  • Optional

Array

  • The array object the current element belongs to
  • Optional

Value

  • A value to use as this when executing a callback function
  • If this parameter is empty, the value “undefined” will be passed as its this value
  • Optional

Return Value

The return value is a new array containing the elements that passed the test. If no elements passed the test, the return value will be an empty array.

Examples

Happy coding!

More Info!

JavaScript docs