Friday, May 31, 2019

difference between map and filter in javascript and how map and filter works in javascript

if you don’t know what functional programming using java script, you’ve probably been using map and filter just because they’re so very useful for your code lets know some basics of how map and filer works and whats the difference between them with example :

javascripterrs blogspot
difference between map and filer in javascript

Description:

The map() method creates a new array with the results of calling a provided function on every element in the calling array.
Above description provided by MDN

Example:
const numbers = [12, 14, 8, 100];
const halves = numbers.map(x => x / 2);
// Console.log(halves)
// halves is [6, 7, 4, 50]

 map is used when you have an array of data( Like Math Term) and you want manipulate something ( another Math term with it) for every item in that array. Just call map for it . it will do the rest for you.



Description:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.
Above description provided by MDN

Example:
const words = ["spray", "limit", "elite", "silverin", "warriors", "knowledge"];

const longWords = words.filter(word => word.length > 6);
// longWords is ["silverin", "warriors", "knowledge"]

like same map ‘filter’ is nothing special with it. It’s going over values in ‘string’ array checking each values in the array  and which  pass the condition, in this case which value has length greater than 6 and then returns a new array with those passed values.

Comment Support:



We gone through some really cool and creative uses of map and filter… And we are ready to go through your queries with map and filter so please feel free to pass your messages to  the comments, I’m sure there are Plenty ways to gave map and filter we made our best to differentiate both with example .

No comments:

Post a Comment