DEV Community

DIWAKARKASHYAP
DIWAKARKASHYAP

Posted on • Edited on

Filter() method in JavaScript ?

Filter() is a method in JavaScript that can effortlessly provide filtered output data(in the form of array) by processing an array

Here's the syntax of the filter() method:

array.filter(function(currentValue, index, arr), thisArg)
Enter fullscreen mode Exit fullscreen mode

In this syntax:

Parameters:

function(currentValue, index, arr): Required. A function to test each item in the array. The function should return true for the items that meet the specified condition, and false otherwise.

  • currentValue: The current element being processed in the array.
  • index: Optional. The index of the current element being processed in the array.
  • arr: Optional. The array on which filter() was called.
  • thisArg: Optional. An object to which the this keyword can refer in the callback function. If this parameter is empty, the this keyword is undefined in the callback function.

Here is a basic usage example:


const array = [1, 2, 3, 4, 5, 6];

const filteredArray = array.filter(element => element > 3);

console.log(filteredArray); // Output: [4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

In this case, the test function checks whether each element in array is greater than 3. The filter() method returns a new array filteredArray containing only the elements that passed this test.

Thank you for reading. I encourage you to follow me on Twitter where I regularly share content about JavaScript and React, as well as contribute to open-source projects. I am currently seeking a remote job or internship.

Twitter: https://twitter.com/Diwakar_766

GitHub: https://github.com/DIWAKARKASHYAP

Portfolio: https://diwakar-portfolio.vercel.app/

Top comments (7)

Collapse
 
gkucmierz profile image
Grzegorz Kućmierz

Nothing is required in function arguments.
You can for example build function, that takes 50% random elements and don't look at current value.

const takeRandom = percent => () => Math.random() > percent;
const random50 = takeRandom(0.5);
Enter fullscreen mode Exit fullscreen mode

instacode playground

Collapse
 
diwakarkashyap profile image
DIWAKARKASHYAP

thank you for comment
you use Math.random() instead of current value and also this is not a real world example you use and to clear more , try to run array.filter() without any argument .

Collapse
 
miketalbot profile image
Mike Talbot ⭐

I use that method to randomly select sub lists (with a seeded random, but not using the element in the array). Similar techinques are used to select every other entry, every nth entry etc. In none of these is the element needed.

Collapse
 
gkucmierz profile image
Grzegorz Kućmierz

Image description

developer.mozilla.org/en-US/docs/W...

Element in callbackFn is not required.

You wrote this:

Image description

Thread Thread
 
diwakarkashyap profile image
DIWAKARKASHYAP

where they are writing (Optional) ?
check this

Image description

w3schools.com/jsref/jsref_filter.asp

The currentValue parameter is required in the filter() function because it represents the element that is currently being processed in the array. It's the element on which the test function (callback function) is applied.

When you call filter() on an array, JavaScript runs the callback function on each element of the array, one by one, in order. The value of currentValue changes each time the callback function runs - it's the specific element in the array that's currently being processed.

So, currentValue is essential because it's the item you are testing in your callback function to determine whether it should be included in the output array.

Thread Thread
 
gkucmierz profile image
Grzegorz Kućmierz • Edited

I agree with you that currentValue is essential here, but still it is not required.
What that even mean?
Arguments in function declaration in js are never required.

Look at tc39 specs here: tc39.es/ecma262/#sec-array.prototy...

Btw. thank you for mentioning w3schools - there is misinformation in their specs.

I wrote to them.

Thread Thread
 
diwakarkashyap profile image
DIWAKARKASHYAP

you are right , i changed the blog . thank you for correct me