In this section we are picking up what is left from Part1,
initialValue and currentIndex parameter in .reduce()
, as well as some of .reduce()
's application.
Fig. 1; from MDN
Regarding whether the initial value is set, there is connection between initialValue and the start point of currentIndex (see Fig. 1 "currentIndex"), though not fully manipulatable:
Fig. 2; from MDN
Fig. 3; from MDN
When .reduce()
is called without an initialValue, currentValue is set to go from "index(currentIndex) 1 "
(arr[1]
; 1 in our example) in the first rotation of the loop,
where the one that is set with an initialValue "10" is going from "index(currentIndex) 0" (arr[0]
; 0) in the first call.
Aside from adding up the values in a plain array of numbers, .reduce()
can also be used to
1.) Calculate the sum of the values of a certain key in an object array, or
2.) Counting instances of values in an object
...more application of .reduce()
can be found on MDN