Tech Blog

Coding

Map, Filter, Reduce in Javascript

By Priyanka S2 min read
← Back to blog

Overview: 

 .map(), .filter(), .reduce() are all array methods in javascript. Each method will iterate over an array and perform transformations or computation. Each will return a new array based on the result of the function. Lets see how to use each of these methods in this article.

Map :

The map() is a method that transforms an array according to the applied function and returns the updated output. It works on each element of an array. 

Syntax :

 array.map(callback[,object])

Example 1 

Double each elements in an array.: 

Example 2:

Binary of each element in an array:

Filter:

The filter() method takes each element in an array and it applies a conditional statement against it. If the conditional returns true, it will get pushed into an output array , else it will not.

Example 1:

Find odd numbers in an array.

Example 2:

Numbers greater than 4 in array

The syntax of filter() is similar to map(), except the callback function should return true to keep the element or not 

Reduce:

The reduce() method reduces an array of values down to just one value. It runs a reducer function on each element of the array to get the output value.

Example 1:

Sum of the array:

Example 2:

Find maximum number in an array

Conclusion:

We can try to replace some of our for loops with .map(), .filter(), .reduce() where it seems to fit. The code will become much easier to read.

Keep reading

You may be interested in

Browse all articles →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Remote Access to Microsoft SQL Database Server
April 5, 20232 min read

Remote Access to Microsoft SQL Database Server

Remote access to a SQL server is important because it allows users to access the server from anywhere, at any time. This enables remote workers to access the data they need when they need it, even if they are not physically in the same location as the server. It also allows IT admins to access […]

By Malkeet SinghRead More →
Types of Load Balancer in AWS
February 6, 20235 min read

Types of Load Balancer in AWS

What is Load Balancing ? Load balancing is a critical component of any scalable and highly available Cloud or On-premise architecture. It helps distribute incoming traffic across multiple servers, improving the availability and scalability of a website or application. In the cloud, load balancing is offered as a service by various cloud providers such as […]

By Keerthik ShenoyRead More →