1reduce
The reduce method of handwritten implementation of the array,
common usage
reduce will pass in the second parameter B20 as our first parameter, which is the first parameter of function A, and then traverse our array, call our function A in turn for the value of the array, and call it once when we get a value. , And continue to call the last value returned as the first parameter of A.
The idea is clear, it is easy, that is, the function A that we passed in is called internally to help us multiple times.
As shown in the figure
, the pre we passed in for the first time is used as the result, and then the array is traversed, and then the result value obtained is the first in turn. Each parameter, the value of each traversal is passed in as the second parameter. The business logic is written in function A by ourselves. It just traverses and calls for us. Is it very simple.