[Go to site: main page, start]

0% found this document useful (0 votes)
3 views7 pages

JavaScript Array Methods Explained

Uploaded by

alh94626
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

JavaScript Array Methods Explained

Uploaded by

alh94626
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

@NourheneAbbes

Map(), Filter(),
Reduce(),
Find()

1
[Link]():
Takes an array returns
transforms each item
a new array
Const numbers =[1,2,3];
Const doubled =[Link](num => num*2);
[Link](doubled) ;

Why use map ?

when you want to create a new version of the


array where each item is modified
2
[Link]():
Takes an array checks each item returns an

new array with items that pass the test


Const numbers =[1,2,3,4];
Const even =[Link](num => num % 2 === 0);
[Link](even)

Why use filter?


when you want to keep only some items based
on a condition 3
[Link]():
returns one
Takes an array runs a function for
final value
each item

Const numbers =[1,2,3,4];


Const total=[Link]((sum , num) => sum + num ,0);
[Link](total)

Why use reduce?


when you want to get one result (like a sum, count,or
merged object) from many items 4
[Link]():
Takes an array runs a predicate for each item
returns the first item that matches a condition( or undefined
if none does )
Const users = [
{ name: "Alice" },{ name: "Bob" },{ name: "Charlie" }];
const user = [Link](u => [Link] === "Bob");

Why use find?


when you want one item from the array --the first one –
5
that satisfies a condition.
Summary
Method Use When You Want To .. Output Type

map() Change every item New Array

filter () Keep only items that match a rule New Array

reduce () Combine all into a single value Single Value

find() Get the first item that Single element (or undefined)
matches a condition

6
FOLLOW FOR MORE

You might also like