Functional programming in JavaScript using map() and filter()

Today I’m going to make you familiar with Map & Filter ( array functions ) in JavaScript which will make your code less chunky, simple and clean ( less buggy) . 

map()

The Map() function takes an array and creates a new array by applying the function to each and every array’s element. Say if you have an array of decimal numbers and you want an array of rounded-off numbers, then there are multiple ways to do it .

 You can achieve this by using for ( or foreach , for..in) loop and apply function to each element of an array as shown below :

 Let’ see the implementation of the Map() function in the above example  . 

filter()

In simple terms, filter() is used to filter out some elements based on some conditions from an array and stored in an array. If the given condition is true then it will push the element in the new array otherwise it will not push the element.

This can be implemented by using for ( or foreach , for..in) loop as shown below.

Let’s see the above example with implementation of filter() : 


And That’s all. 

Thank you for reading.

1 Response

  1. July 3, 2020

    […] more about functional programming using arrays in […]

Leave a Reply

%d bloggers like this: