reduce()
Method receives a function as an accumulator (accumulator), and each value (from left to right) in the array begins to merge and eventually a value.
concept: invokes the specified callback function for all elements in the array. The return value of the callback function is the cumulative result, and this return value is supplied as an argument the next time the callback function is called.
Grammar:
Array1.reduce (callbackfn[, InitialValue])
Parameters:
Parameters |
Defined |
array1 |
required. |
callbackfn |
required. reduce method will call CALLBACKFN function once. |
InitialValue |
Optional. if initialvalueis specified, it is used as the initial value to start the build. the first call to the CALLBACKFN function will provide this value as a parameter rather than as an array value. |
return value
The cumulative result obtained by the last call to the callback function.
Abnormal
A TypeError exception is thrown when either of the following conditions is true:
The callbackfn parameter is not a function object.
The array does not contain elements, and InitialValueis not provided.
Note
If InitialValueis provided, the reduce method invokes the CALLBACKFN function (in ascending index order) once for each element in the array. if InitialValueis not provided, the reduce method invokes the CALLBACKFN function on each element starting with the second element.
The return value of the callback function is provided as the previousvalue parameter The next time the callback function is called. The return value obtained from the last call to the callback function is the return value of the reduce method.
The callback function is not called for elements that are missing from the array.
Attention
The Reduceright Method (Array) (JavaScript) processes elements in descending index order.
callback function syntax
The syntax for the callback function is as follows:
function Callbackfn (Previousvalue, CurrentValue, Currentindex, Array1)
You can declare a callback function with up to four parameters.
The following table lists the callback function parameters.
Callback Parameters |
Defined |
previousvalue |
The value obtained by the last call to the callback function. reduce method provides InitialValue , When the function is first called, previousvalue is initialvalue . |
CurrentValue |
The value of the current array element. |
Currentindex |
The numeric index of the current array element. |
Array1 |
The array object that contains the element. |
Call the callback function for the first time
When the callback function is first invoked, the value supplied as a parameter depends on whether the reduce method has the initialvalue parameter.
If you provide InitialValueto the reduce method:
If InitialValueis not provided:
Modifying an Array object
An array object can be modified by a callback function.
The following table describes the results obtained from modifying an array object after the reduce method starts.
Conditions after the reduce method is started |
Whether the element is passed to the callback function |
Adds an element outside the original length of the array. |
Whether. |
Add elements to fill the missing elements in the array. |
Yes, if the index has not been passed to the callback function. |
element is changed. |
Yes, if the element has not been passed to the callback function. |
Removes an element from the array. |
No, unless the element has been passed to the callback function. |
If you simply look at the concept, a look will be dizzy, but look at the sample demo and output, the role of reduce is at a glance.
Example
[0,1,2,3,4].reduce (function (Previousvalue, currentvalue, index, array) { return Previousvalue + currentvalue;}); //10 [0,1,2,3,4].reduce (function (Previousvalue, currentvalue, index, array) { return previousvalue + currentvalue;},10); //20 [1].reduce (function ( Previousvalue, currentvalue, index, array) { return previousvalue + currentvalue; },20); //21 // If the array has only one element (regardless of location) and no initialvalue, is provided or initialvalue is provided but the array is empty, then this unique value will be returned and callback will not be executed. [There is at least one value of have ] [1].reduce (function (Previousvalue, currentvalue, index, array) { return previousvalue + currentvalue; }); //1 [].reduce (function (Previousvalue, currentvalue, index, array) { return previousvalue + currentvalue;},2);//2 [null].reduce (function (previousValue, Currentvalue, index, array) { return previousvalue + currentvalue;},2);//2 //if a value of undefined is present, the output nan [undefined].reduce (function (Previousvalue, currentvalue, index, array) { return previousvalue + currentvalue;},2);//NaN [1].reduce ( function (Previousvalue, currentvalue, index, array) { return previousvalue + currentvalue;},undefined); nan //if the array is empty and no initialvalue, are provided typeerror [two values are not] [].reduce (function ( Previousvalue, currentvalue, index, array) { return previousvalue + CurrentValue;}); /uncaught typeerror: reduce of empty array with no initial value ( ...) {}.reduce (function (previousvalue, currentvalue, index, array) {RETURN&Nbsp;previousvalue + currentvalue;},10); {}.reduce (function (Previousvalue, currentvalue, index, array) {return previousvalue + currentvalue;}); //Uncaught SyntaxError: Unexpected token . example: Adding an array of all items var Total = [0, 1, 2, 3].reduce (function (a, b) { return a + b;}); console.log (total);// total == 6 Example: array flattening var flattened = [[0, 1], [2, 3], [4, 5]].reduce (function (a, b) { return a.concat (b);}); Console.log (flattened);// flattened is [0, 1, 2, 3, 4, 5]
Reference: http://www.cnblogs.com/leejersey/p/5466091.html
https://msdn.microsoft.com/library/ff679975 (v=vs.94). aspx
This article is from the "Apple Skins Front End" blog, so be sure to keep this source http://beileixinqing.blog.51cto.com/7540036/1937213
Es 5 array Reduce method memory