![]() The final value stored in accumulator will be an array that contains, which is the same array we got when we chained together the filter and map methods in earlier examples. This pattern continues until we have iterated through the entire array. Now, accumulator is no longer an empty array and contains one value. Therefore, we double this value to 2 and push this number to accumulator. Therefore, the if condition fails and we simply return the accumulator which is still set equal to an empty array.ĭuring the second iteration, currentValue is now equal to 1, which is greater than 0. During the first iteration of the reducer method, currentValue will be equal to -1. We check if currentValue is greater than zero. Now, let's look at the reducer, the callback function we pass into the reducer method. This value is set to an empty array, which means the accumulator will be equal to an empty array during the first iteration of the reduce method. The most important thing to notice is that we are passing in initialValue as the second parameter of the reduce method. Let's dive into what's happening in the code snippet above. reduce ( ( accumulator, currentValue ) =>, initialValue ) Copied! ⭐ const array = const initialValue = // Iterates through six elements const newArray = array.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |