What can reduce () in JavaScript do

Tram Ho

Hi guys, continuing on the topic of JavaScript today, we will look at a method that can be considered the most cumbersome when working with array js that is reduce() . In addition to the most typical example is “summing”, then what can reduce() can do, …. Let me learn through this article.

Again, reduce() used to execute a function on each element of the array (from left to right) with a cumulative variable to return a unique value.

  • reduce () DOES NOT change the original array
  • Reduce () returns the value after reduction

Syntax

Here are some examples also called snippets extremely beneficial to help you during your work.

1. Calculate the sum, difference, product, quotient …

2. Replace Array.prototype.reverse()

3. Replace Array.prototype.map()

4. Replace Array.prototype.filter()

5. Replace Array.prototype.some()

6. Replace Array.prototype.every()

7. Find the difference between the 2 arrays

8. Split up the array

9. Contrast with the 8th guy

10. Eliminate duplicate elements

Conclusion

It’s also quite long, reading a lot is also painful =)) I pause the article here, see you in the next section with interesting things with reduce()

If you find a good article, give me +1 upvote. If you like me, please press the follow button for more cool things. Good luck !

Share the news now

Source : Viblo