ECMAScript5 defines two merge methods for arrays: reduce() and reduceRight(). Both methods iterate over the arbitrary items of the array and then build a final returned value. The reduce() method starts from the first item of the array and iterates through the end of the array one by one. The reduceRight() method is just the opposite. It starts from the last item of the array and goes forward to the first item.
Both methods receive two parameters: a function called on each item and the initial value as the basis for the merge. The function passed to reduce() and reduceRight() receives 4 parameters: the previous value, the current value, the index of the item, and the array object. Any value returned by this function will be automatically passed to the next item as the first parameter. The first iteration occurs on the second term of the array, so the first parameter is the first term of the array, and the second parameter is the second term of the array.
For example, we can use the reduce() method to find the sum of all values in an array.
var nums = [1,2,3,4,5];var sum = nums.reduce(function(prev,cur,index,array){ retrun pre + cur;});In the above code, the first time the callback function is executed, prev is 1 and cur is 2. The second time prev is 3 and cur is 3. The reduce() method will repeat this process until each item in the array is accessed once, and finally assign the returned result to sum.
The reduceRight() method is similar to the left and right, but is executed in reverse. For example:
var nums = [1,2,3,4,5];var sum = nums.reduceRight(function(prev,cur,index,array){ retrun pre + cur;});In this example, the first time the callback function is executed, prev is 5 and cur is 4. The final sum result is the same as the reduce() method, both 15.
Browsers that support reduce() and reduceRight() are: IE9+, Firefox3+, Safari4+, Opera10.5+ and Chrome.
Replenish
ECMAScript5 also adds 2 new methods to merge arrays: reduce() and reduceRight().
Both of these iterate over all items in the array
reduce(): Iterate one by one from the first item to the end.
reduceRight(): Starting from the last item of the array, iterating over to the first item of the array.
Both methods accept two parameters: the function called on each item (parameters are: previous value, current value, index of item, array object)
Any value returned by this function will be automatically passed to the next item as the first parameter. The first iteration occurs on the second item of the array,
Therefore, the first parameter is the first item of the array, and the second parameter is the second item of the array
and as the initial value of the merge base.
Use the reduce() method to perform the sum of all values in an array, such as:
var values = [1, 2, 3, 4, 5]; var sum = values.reduce(function (prev, cur, index, array) { return prev + cur; }); alert(sum); //The result is the same, but the direction is the opposite var sum2=values.reduceRight(function (prev,cur,index,array) { return prev+cur; }); alert(sum2);Merge sort is an effective sorting algorithm based on merge operation. This algorithm is a very typical application of Divide and Conquer.
Merge sorting method is to merge two (or more than two) ordered tables into a new ordered table, that is, divide the sequence to be sorted into several subsequences, each subsequence is ordered. Then the ordered subsequences are merged into the overall ordered sequence.
Merge sorting is an effective sorting algorithm based on merge operation. This algorithm is a very typical application of Divide and Conquer. Merge the ordered subsequences to obtain a completely ordered sequence; that is, make each subsequence first order, and then make the subsequence segments order. If two ordered tables are merged into one ordered table, it is called 2-way merge.