![]() Using reduce() method to get a String from an Array When this occurs, the code updates the value for the ‘blue’ property name to 2, and it will continue to do so for additional cases where a color is repeated.įor more about the if…else statement check out this link. ![]() The if statement only comes into play when we encounter a color for a second time – in this case, when we see ‘ blue’ for the second time. This occurs as each iteration is hitting the else statement above. The following example uses Array.reduce() to sum the contents of an array of integers: const q4th = Ĭonst sum = q4th.reduce(function (accumulator, monthlySales) We’ll then compare it with the Syntax for reduce(). This article will explore both of these features of the reduce method.įirst, let’s take a look at a basic reduce() method example. You get to choose the data type to be returned.The Array object’s reduce method is an iteration method which, similar to other iteration methods ( map(), forEach(), filter() etc.) helps us work through all of the elements of a dataset, performing actions on each one.Īrray reduce has two very powerful features:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |