Basic concepts:
The reduce () method receives a function as an accumulator, and each value (from left to right) in the array begins to shrink, resulting in a value.
Reduce each element in the array to execute a callback function in turn. Does not include elements that are deleted or never assigned in the array, and accept two parameters. The first parameter is a callback function and receives four parameters: the initial value (or the return value of the last callback function), the current element value, the current index, and the array of reduce. The second parameter is the initial parameter, which is the first parameter that is called callback for the first time.
Grammar:
Arr.reduce (callback, [InitialValue])
* Callback (executes every worthwhile function in the array, including four parameters)
Previousvalue (the value returned by the last call back, or the provided initial value (InitialValue))
CurrentValue (the element currently being processed in the array)
Index (the current element is indexed in the array)
Array (calls to the arrays of reduce)
* Inatialvalue (as the first parameter of the first call to callback)
Simple Application
As you can see, the reduce function continuously stacks according to the initial value of 1, completing the simplest recursion
The result type of the return value of the reduce function is the same as the initial value passed in, and the initial value in the previous instance is the number type, so the initial value can also be an object type
Advanced Applications
using the Reduce method, you can complete a multidimensional data overlay, such as the initial value {sum:0} in the previous example, which is just a dimension of the operation, if the overlay of multiple attributes is involved, such as: {sum:0, totalineuros:0, totalinyen:0}, The corresponding logic is required for processing.
In the following method, a divide-and-conquer method is used to encapsulate an array with the first parameter of the reduce function callback, and each function in the array is superimposed separately and the reduce operation is completed. Everything is managed by a manager function to manage the process and pass the initial parameters.
Front HIGH ENERGY!!!!! I also spent more than half an hour to understand the study, has been added to the comments, hope to help you, interested can study under
With the implementation of the manager function above, he needs to reducers the object as a parameter and return a function of type callback, which, as the first parameter of reduce, performs multidimensional overlay work (Object.key ()) inside the function. With this idea of divide and conquer, multiple attributes of an object can be superimposed simultaneously.
One more example: The final result of a classmate is shown below
How to ask the student's overall grade?
Assume that the students because of disciplinary punishment in the total deduction 10 points, only the initial value of 10 can be.
The following example adds a bit of difficulty. Assuming that the student's total scores, each branch accounted for a different proportion, respectively: 50%, 30%, 20%, how should we find the final weight result?
Another example is how to know the number of occurrences of each letter in a string of strings
Since the type initial value of the overlay result can be set by the second parameter, reduce is not just an addition, we can use it flexibly to convert all kinds of types into objects, or we can convert one form of an array to another form of an array. You can try it yourself.
KOA (node's framework) source code, there is a only module, the entire module is a simple return to the reduce method operation of the object:
Hope that the above content will have a deeper understanding of you on reduce, thank you!
Array reduce methods and advanced techniques