DEV Community

Cover image for Map, Reduce, Filter Methods in Array
Vigneshwaran V
Vigneshwaran V

Posted on

Map, Reduce, Filter Methods in Array

In JavaScript, map(), filter(), and reduce() are built-in array methods used to transform, filter, and aggregate data without mutating the original array. They form the backbone of clean, functional programming in JavaScript.

map()

Use map() when you want to apply the same operation to every item in an array and get a new array of the same length back.

  • Used to transform every element of an array and return a new array.

Example

let nums = [1, 2, 3, 4];

let result = nums.map(function(num) {
    return num * 2;
});

console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output

[2, 4, 6, 8]
Enter fullscreen mode Exit fullscreen mode

What happened?

1 → 2
2 → 4
3 → 6
4 → 8
Enter fullscreen mode Exit fullscreen mode

Original array

[1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

New array

[2, 4, 6, 8]
Enter fullscreen mode Exit fullscreen mode

filter()

  • The filter() method creates a new array filled with elements that pass a test provided by a function.

  • The filter() method does not execute the function for empty elements.

  • The filter() method does not change the original array.

  • Used to select elements that satisfy a condition.

Example

let nums = [1, 2, 3, 4, 5, 6];

let even = nums.filter(function(num) {
    return num % 2 === 0;
});

console.log(even);
Enter fullscreen mode Exit fullscreen mode

Output

[2, 4, 6]
Enter fullscreen mode Exit fullscreen mode

What happened?

1 x
2 ✔
3 x
4 ✔
5 x
6 ✔
Enter fullscreen mode Exit fullscreen mode

Only the elements for which the condition returns true are kept.


reduce()

  • The reduce() method executes a reducer function for array element.

  • The reduce() method returns a single value: the function's accumulated result.

  • The reduce() method does not execute the function for empty array elements.

  • The reduce() method does not change the original array.

  • Used to combine all elements into a single value.

Example

let nums = [1, 2, 3, 4];

let sum = nums.reduce(function(acc, num) {
    return acc + num;
}, 0);

console.log(sum);
Enter fullscreen mode Exit fullscreen mode

Output

10
Enter fullscreen mode Exit fullscreen mode

Step-by-step

Initial:

acc = 0
Enter fullscreen mode Exit fullscreen mode

Iteration 1:

acc = 0 + 1 = 1
Enter fullscreen mode Exit fullscreen mode

Iteration 2:

acc = 1 + 2 = 3
Enter fullscreen mode Exit fullscreen mode

Iteration 3:

acc = 3 + 3 = 6
Enter fullscreen mode Exit fullscreen mode

Iteration 4:

acc = 6 + 4 = 10
Enter fullscreen mode Exit fullscreen mode

Final result:

10
Enter fullscreen mode Exit fullscreen mode

Note

  • At the first callback, there is no return value from the previous callback.

  • Normally, array element 0 is used as initial value, and the iteration starts from array element 1.

  • If an initial value is supplied, this is used, and the iteration starts from array element 0.


map() → "Modify each item"
filter() → "Keep only matching items"
reduce() → "Reduce everything to one value"


reduceRight()

  • The reduceRight() method executes a reducer function for each array element.

  • The reduceRight() method works from right to left.

  • The reduceRight() method returns a single value: the function's accumulated result.

  • The reduceRight() method does not execute the function for empty elements.

Example

const numbers = [45, 4, 9, 16, 25];

let sum = numbers.reduceRight(myFunction);

function myFunction(total, value, index, array) {
  return total + value;
}

console.log(sum);
Enter fullscreen mode Exit fullscreen mode

Output

99
Enter fullscreen mode Exit fullscreen mode

How reduceRight() works
It starts from the right side of the array:

[45, 4, 9, 16, 25]
             ↑ start
Enter fullscreen mode Exit fullscreen mode

Since no initial value is given:

total = 25
Enter fullscreen mode Exit fullscreen mode

and iteration starts with:

value = 16
Enter fullscreen mode Exit fullscreen mode

Final result:

99
Enter fullscreen mode Exit fullscreen mode

The difference between reduce() and reduceRight() is only the direction:

  • reduce() → left to right

  • reduceRight() → right to left

For addition, both give 99, but for some operations (like string concatenation), the result can be different.

Note

  • At the first callback, there is no return value from the previous callback.

  • Normally, the last array element is used as initial value, and the iteration starts from the element before.

  • If an initial value is supplied, this is used, and the iteration starts from last element.


Rest Operator (...) and spread (...) operators

The rest (...) and spread (...) operators use the same symbol (...), but they do different jobs depending on where they are used.

1. Rest Operator (...)

Rest means:
Collect multiple values into a single array.

Example:

function add(...nums) {
    console.log(nums);
}

add(10, 20, 30, 40);
Enter fullscreen mode Exit fullscreen mode

Output

[10, 20, 30, 40]
Enter fullscreen mode Exit fullscreen mode

What happened?

10, 20, 30, 40
Enter fullscreen mode Exit fullscreen mode

are collected into

nums = [10, 20, 30, 40]
Enter fullscreen mode Exit fullscreen mode

So ...nums is called the rest parameter.

2. Spread Operator (...)

Spread means:
Expand an array (or object) into individual values.

Example

let nums = [10, 20, 30];

console.log(...nums);
Enter fullscreen mode Exit fullscreen mode

Output

10 20 30
Enter fullscreen mode Exit fullscreen mode

What happened?

[10, 20, 30]
Enter fullscreen mode Exit fullscreen mode

becomes

10, 20, 30
Enter fullscreen mode Exit fullscreen mode

Copy an Array

let arr1 = [1, 2, 3];

let arr2 = [...arr1];

console.log(arr2);
Enter fullscreen mode Exit fullscreen mode

Output

[1, 2, 3]
Enter fullscreen mode Exit fullscreen mode
  • Rest Operator (...): Collects multiple values into a single array.

  • Spread Operator (...): Expands an array or object into individual elements/properties.

Reference

https://www.w3schools.com/jsref/jsref_map.asp
https://www.w3schools.com/jsref/jsref_reduce.asp
https://www.w3schools.com/jsref/jsref_filter.asp

Top comments (1)

Collapse
 
kamalesh_ar_6252544786997 profile image
Kamalesh AR

very understandable one👍🏻