0% found this document useful (0 votes)
114 views

JS - Map-Filter-Reduce

This document summarizes 3 array methods in JavaScript - map(), filter(), and reduce(). It explains that map() returns a new array by applying a function to each element, filter() returns an array containing elements that pass a test, and reduce() runs a function on each element to produce a single value. It provides examples and descriptions of the parameters for each method.

Uploaded by

Josie DeLima
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

JS - Map-Filter-Reduce

This document summarizes 3 array methods in JavaScript - map(), filter(), and reduce(). It explains that map() returns a new array by applying a function to each element, filter() returns an array containing elements that pass a test, and reduce() runs a function on each element to produce a single value. It provides examples and descriptions of the parameters for each method.

Uploaded by

Josie DeLima
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

JS

3 KILLER
ARRAY
METHODS
map( )

filter( )

Ajay Yadav
reduce( )
@ATechAjay
map( )
It accepts a callback function once for
each array element and returns a new
array.

The map( ) method doesn't execute the


function for an empty array.

Ajay Yadav
@ATechAjay
item - It's a required parameter, that is
the value of the current array item.

index - Optional, it is the current index of


the array element that is being processed.

arr -Optional, the array on which the


map( ) method was called.

[ ''IIndex
ndex10Square:
Square:400'
100',,

''IIndex 2 Square: 900' ,

''IIndex 34 Square: 1600' ,


,

ndex Square: 2500'


ndex 5 Square: 3600' ]

The new resultant array will be square


variable.
Ajay Yadav
@ATechAjay
filter( )

The filter() method returns an array that


passes the condition. If no elements pass
the condition, it returns an empty array.

It doesn't execute the callback function


for empty elements.

Ajay Yadav
@ATechAjay
item - It's a required parameter, that is
the value of the current array item.

index - Optional, it is the current index of


the array element that is being processed.

arr - Optional, the array on which the


filter( ) method was called.

Ajay Yadav
The filtered array will be nameStartsWithA variable.
@ATechAjay
reduce( )

The reduce( ) method executes a


reducer function on each element of the
array.

It returns a single value as a result and


doesn't execute the function for an empty
array element.

Ajay Yadav
@ATechAjay
accumulator - It's a value of the previous
function call, or the InitialValue for the first call.

If there is no any InitialValue provided then it’s


accumulate the first item of the array and item is
initialized to the second value in the array.

item - It's a required parameter, that is the


value of the current item of the array.

index - Optional, it is the current index of the


array element that is being processed.

arr - Optional, the array on which the reduce( )


method was called.

InitialValue - Optinal, It’s a starting value that is


used to initialize the callback function for the first
time.
Ajay Yadav
@ATechAjay
For the 1st iteration:

total = 0(InitialValue)

item = 10

InitialValue = 0

Ajay Yadav
@ATechAjay
In the above code, I’ve omitted the
InitialValue value, so the total variable is
initialised with 10 and the item variable is
initialised with 20.

It returns a single value that is stored in a


variable called totalINR.
NOTE: Array methods does not changed the original array.
Ajay Yadav
@ATechAjay
Thanks for the reading
Follow me for ultimate content

on HTML, CSS, JavaScript,


React, DSA, and Git

Ajay Yadav
@ATechAjay

You might also like