Blog

JS

Array

map, filter, & reduce

The three must know array methods

Ragav Kumar V

Ragav Kumar V

Aug 25, 2022 — Updated Dec 8, 2022 · 4 min read

  1. It always returns copy of array
  2. sourceArray.length === outputArray.length -> true
  3. Transforms data type

_7
// eg:1
_7
const nums = [10, 5, 15];
_7
const squares = nums.map((num) => num * num);
_7
console.log(squares);
_7
// [ 100, 25, 225 ]
_7
console.log(nums);
_7
// [ 10, 5, 15 ]

  1. nums remains unchanged
  2. nums.length equals squares.length
  3. nums (Array of numbers) -> squares (Array of numbers)
  4. Transformation was not need here

_14
// eg:2
_14
const avengers = [
_14
"Hulk",
_14
"Iron man",
_14
"Black widow",
_14
"Captain america",
_14
"Spider man",
_14
"Thor",
_14
];
_14
_14
// Array of Strings -> Array of numbers
_14
const letterCount = avengers.map((name) => name.length);
_14
console.log(letterCount);
_14
// [4, 8, 11, 15, 10, 4]

  1. avengers remains unchanged
  2. avengers.length equals letterCount.length
  3. nums (Array of strings) -> squares (Array of numbers)

  1. It always returns copy of the array
  2. sourceArray.length >= outputArray.length -> true
  3. input data type === output data type

_5
// eg:1
_5
const marks = [90, 40, 25, 85, 10, 0, 99];
_5
const passedMarks = marks.filter((mark) => mark >= 40);
_5
console.log(passedMarks, marks);
_5
// [90, 40, 85, 99]

  1. marks remains unchanged
  2. marks.length is greather than marks.length
  3. marks (Array of numbers) >= passedMarks( Array of numbers) -> true

_14
// eg:2
_14
const avengers = [
_14
"Hulk",
_14
"Iron man",
_14
"Black widow",
_14
"Captain america",
_14
"Spider man",
_14
"Thor",
_14
];
_14
_14
// Array of strings -> Array of strings
_14
const largeAvengerNames = avengers.filter((name) => name.length > 10);
_14
console.log(largeAvengerNames);
_14
// ["Black widow", "Captain america"]

  1. avengers remains unchanged
  2. avengers.length is greather than largeAvengerNames.length
  3. avengers (Array of strings) >= largeAvengerNames( Array of strings) -> true
  1. Dot chaining can continue until the array methods returns new array

_37
const scores = [
_37
{
_37
marks: 32,
_37
name: "Yvette Merritt",
_37
},
_37
{
_37
marks: 57,
_37
name: "Lillian Ellis",
_37
},
_37
{
_37
marks: 22,
_37
name: "Mccall Carter",
_37
},
_37
{
_37
marks: 21,
_37
name: "Pate Collier",
_37
},
_37
{
_37
marks: 91,
_37
name: "Debra Beard",
_37
},
_37
{
_37
marks: 75,
_37
name: "Nettie Hancock",
_37
},
_37
{
_37
marks: 20,
_37
name: "Hatfield Hodge",
_37
},
_37
];
_37
_37
const passedStudentsNames = scores
_37
.filter((student) => student.marks >= 40)
_37
.map((student) => student.name);
_37
_37
console.log(passedStudentsNames);
_37
// ["Lillian Ellis", "Debra Beard", "Nettie Hancock" ]

  1. filter and map always returns array
  2. Hence we could keep chaining untill array method returns array
  1. Array -> any data type

_6
// eg:1
_6
const sum = [1, 7, 3, 10, 5].reduce((acc, curr) => acc + curr, 0);
_6
console.log(sum); // 26
_6
_6
const sum1 = [1, 7, 3, 10, 5].reduce((acc, curr) => acc + curr);
_6
console.log(sum1); // 26

initial value -> 0 hence acc value in first loop is 0

acccurrresult (acc + curr)
011
178
8311
111021
21526

_4
// eg:2
_4
_4
const sum1 = [1, 7, 3, 10, 5].reduce((acc, curr) => acc + curr);
_4
console.log(sum1); // 26

Since no initial value hence acc -> first value & curr -> second value in array

acccurrresult (acc + curr)
178
8311
111021
21526

_7
// eg:3
_7
const total = scores
_7
.map((stu) => stu.marks)
_7
.reduce((acc, curr) => acc + curr, 0);
_7
const avg = total / scores.length;
_7
console.log(total, avg.toFixed(2));
_7
// 318 '45.43'

@ragavkumarv
swipe to next ➡️