Elementary Introduction to JavaScript Merging method _ Basic knowledge

Source: Internet
Author: User
Tags prev

ECMAScript5 also added 2 methods for merging arrays: reduce () and reduceright ().

All the entries for both the iterated algebra groups
Reduce (): Iterate from the first entry to the end.
Reduceright (): Begins with the last item of the array, traversing to the first item of the array.

Both methods accept two parameters: the function called on each item (parameter: Previous value, current value, index of item, array object)

Any value returned by this function the fight is automatically passed to the next item as the first argument. The first iteration takes place on the second item of the array,
So the first argument is the first item in the array, and the second argument is the second item of the array.
and the initial value as the base of the merge.
Use the reduce () method to perform operations on the sum of all values in the array, such as:

Copy Code code as follows:

var values = [1, 2, 3, 4, 5];
var sum = values.reduce (function (prev, cur, index, array) {
return prev + cur;
});
alert (sum);
As it turns out, it's just the other way around.
var sum2=values.reduceright (function (Prev,cur,index,array) {
return prev+cur;
});
alert (SUM2);

Merge sort is an efficient sorting algorithm based on merging operations. The algorithm is a very typical application of the partition method (Divide and Conquer).

The merge (merge) Sort method combines two (or more) ordered tables into a new ordered table, which divides the sequence into several subgroups, each of which is ordered. Then the ordered Subsequence is merged into the whole ordered sequence.

Merge ordering is an effective sorting algorithm based on merging operation. The algorithm is a very typical application of the partition method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, that is, the sequence of each subsequence is ordered, and then the sequence between the subsequence segments is ordered. If the two ordered table is merged into an ordered table, it is called 2-way merge.

Related Article

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.