@NourheneAbbes
Map(), Filter(),
Reduce(),
Find()
1
1.map():
Takes an array returns
transforms each item
a new array
Const numbers =[1,2,3];
Const doubled =numbers.map(num => num*2);
console.log(doubled) ;
Why use map ?
when you want to create a new version of the
array where each item is modified
2
2.filter():
Takes an array checks each item returns an
new array with items that pass the test
Const numbers =[1,2,3,4];
Const even =numbers.filter(num => num % 2 === 0);
console.log(even)
Why use filter?
when you want to keep only some items based
on a condition 3
3.reduce():
returns one
Takes an array runs a function for
final value
each item
Const numbers =[1,2,3,4];
Const total=numbers.reduce((sum , num) => sum + num ,0);
console.log(total)
Why use reduce?
when you want to get one result (like a sum, count,or
merged object) from many items 4
4.find():
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 = users.find(u => u.name === "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