Whats filter in javascript | javascript filter explained

ads 3

Whats filter in javascript | javascript filter explained

ARRAY FILTR
The filter () method creates an array filled with all array elements that pass a test (provided as a function).

 NOTE
 filter () does not execute the  function for array elements  without values.

But how it works?

Let's Understand with an example
Suppose you have given an array of ages

 var ages = [32,33,16,40]

TASK IS
You have to print an array from the given array which have age greater then 18
 var ages = [32,33,16,40]

SOLUTION OF TASK
So you can eaithe use for loop and print value under a given condition using if 
else or........
You can use filter () method

 LETS HOW IT WORK
filter () will return you can copy of array under a given condition var new-ages
 =ages.filter(age=>age>=;18);

EXPLAIN
 filter () takes function as argument and it will return you value at given index or you can say it will return value and index pair Data will be return under some condition given inside that argument

Syntax 
array.filter((value,index)> console.log(value,index)) 
Functional argument loops through the array and return data in key value pair.
____________________________________________________
Conclusion 

So thats what I wanted to share with you guys 👦

And thank you for your time and support.

I wish you guys to enjoy your journey.

Also don't hesitate to share this post with your interested ones.


Gracias 
Previous Post Next Post