Javascript merge method and javascript merge

Source: Internet
Author: User

Javascript merge method and javascript merge

ECMAScript5 also adds two methods to merge Arrays: reduce () and reduceRight ().

Both of them iterate all the items in the array.
Reduce (): traverse from the first item to the end one by one.
ReduceRight (): traverses the first entry of the array from the last entry of the array.

Both methods accept two parameters: the function called on each item (the parameter is 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 in the second entry 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 the initial value as the basis for merging.
The reduce () method can be used to perform the operation of the sum of all values in the array, for example:

Copy codeThe Code is as follows:
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 the Merge operation. This algorithm is a very typical application of Divide and Conquer.

The Merge Sorting method combines two (or more) ordered tables into a new ordered table, that is, the sequence to be sorted is divided into several subsequences, each of which is ordered. Then combine the ordered subsequences into the overall ordered sequence.

Merge Sorting is an effective Sorting Algorithm Based on merge operations. This algorithm is a very typical application of Divide and Conquer. Merges ordered subsequences to obtain a fully ordered sequence. That is, first orders each subsequence, and then orders the subsequence segments. If two ordered tables are merged into an ordered table, it is called a 2-way merge.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.