Map, Filter and Reduce explained.

You probably have heard of .map(), .reduce() and .filter().
Map, reduce, and filter are all array methods in JavaScript.In this article, you will learn why and how to use each one.
I will try and explain with examples of how each of these functions w…


This content originally appeared on DEV Community and was authored by namitmalasi

You probably have heard of .map(), .reduce() and .filter().
Map, reduce, and filter are all array methods in JavaScript.In this article, you will learn why and how to use each one.
I will try and explain with examples of how each of these functions works.

map()

map() is used to transform an array. It creates a new array populated with the results of the calling function on every element in the calling array.

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

const output=array.map(element=>
 {
  return element*2;
})

console.log(output);  // [2,4,6,8,10]

In the above code,
array ===calling array
element=>{...} ===calling function

The array elements are being traversed one by one and in each iteration the current element is multiplied by 2. The new value is pushed into a new array and after the traversal is complete we get a new array of elements in the output.

filter()

Filter is used to filter the array. filter() method creates a new array with all the elements that pass the test implemented by the provided function.
If the test condition returns true, the element gets pushed to the output array. If the condition returns false, the element does not get pushed to the output array.

const array=[10,23,55,40,111];

const output=array.filter(element=>
                      {
  return element%2;
})

console.log(output);

In the example above it is filtering out all the odd elements from the array based on the test implemented in the function and pushing them into a new array and then returning the new array.

reduce()

The reduce() method executes a reducer function on each element of the array, resulting in a single output value.
The reducer function takes two-parameter (accumulator, current).

accumulator= the returned value of the previous iteration
current=the current item in the array

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

const output=array.reduce((acc,curr)=>
{
acc=acc+curr
 return acc

},0)

console.log(output);

The above code calculates the sum of the elements of the array.
The initial value of acc is set to 0 as mentioned in the code. On each iteration, the current element is added to acc, and hence at last the total sum is returned.

Conclusion

I really hope that you enjoyed reading this article and learned something from it. If you have any doubt or you think i missed something let me know in the comments. Feel free to like and share the post.
You can connect with me on:
Twitter:Namit Malasi
LinkedIn:Namit Malasi
Github: Namit Malasi


This content originally appeared on DEV Community and was authored by namitmalasi


Print Share Comment Cite Upload Translate Updates
APA

namitmalasi | Sciencx (2021-09-12T13:37:20+00:00) Map, Filter and Reduce explained.. Retrieved from https://www.scien.cx/2021/09/12/map-filter-and-reduce-explained/

MLA
" » Map, Filter and Reduce explained.." namitmalasi | Sciencx - Sunday September 12, 2021, https://www.scien.cx/2021/09/12/map-filter-and-reduce-explained/
HARVARD
namitmalasi | Sciencx Sunday September 12, 2021 » Map, Filter and Reduce explained.., viewed ,<https://www.scien.cx/2021/09/12/map-filter-and-reduce-explained/>
VANCOUVER
namitmalasi | Sciencx - » Map, Filter and Reduce explained.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/12/map-filter-and-reduce-explained/
CHICAGO
" » Map, Filter and Reduce explained.." namitmalasi | Sciencx - Accessed . https://www.scien.cx/2021/09/12/map-filter-and-reduce-explained/
IEEE
" » Map, Filter and Reduce explained.." namitmalasi | Sciencx [Online]. Available: https://www.scien.cx/2021/09/12/map-filter-and-reduce-explained/. [Accessed: ]
rf:citation
» Map, Filter and Reduce explained. | namitmalasi | Sciencx | https://www.scien.cx/2021/09/12/map-filter-and-reduce-explained/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.