If you always do what you've always done, you'll always get what you've always got.
0%
2023-05-28-筆記-When is the Use case Between Map and Reduce
發表於文章字數:3k所需閱讀時間 ≈3 分鐘
前言: In my mind, when I want to do something to API data , my intuition is always comes up with above two methods.Before we started, we can take a look at the purpose of map and reduce method
reduce is a method that will iterate an array and in most of case this method is used to aggregate elements. If I do not give reduce method initial value it will use first elements as initial value
Use cases for reduce
Aggregation:
1 2 3 4 5 6
const numbers = [1, 2, 3, 4, 5];
// Calculate the sum of all numbers using reduce const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);