Detailed explanation of iteration and merge methods in Javascript, and javascript merge

Source: Internet
Author: User

Detailed explanation of iteration and merge methods in Javascript, and javascript merge

Iteration Method

In Javascript, iteration methods are especially important. In many cases, there are actual requirements. javascript provides five Iteration Methods for us to operate on. They are:

Every () applies the given function to each item in the array. If true is returned for each item, true is returned.

Filter () uses the given function for each item in the array to form a new array of items that return true and return

ForEach () applies the given function to each item in the array, but there is no return value

Map () applies the given function to each item in the array and returns the result of each function call to form a new array.

Same () applies the given function to each item in the array. If one item in the array returns true, true is returned.

In the five methods above, they all accept two parameters: the execution function, that is, the function that needs to be operated on each item. This function has three parameters: the value of the array item, the position of the item in the array, and the array object itself. The given scope, given a scope, affects the this object of the given function. For example:

Var values = [5, 6, 7, 8, 9, 10, 11, 12, 13]; function actionfunc (item, index, array) {console. log (this)}; values. every (actionfunc, document); // The document Object is output 6 times to the console

Merge Method

In addition to the iterative method, javascript also provides two merge methods, which are archive merge. These methods, like the name, use the given function to iterate each item in the array, then return a total value. The two merge methods are:

Reduce () applies the given function to each item in the array from the first entry to the last forward, and returns the sum of the results of running the given function for all the items in the array.

ReduceRight () applies the given function reversely from the last entry in the array to the first entry, and then returns the sum of the results of running the given function for all the items in the array.

The two methods above accept two parameters: the execution function, that is, the function that requires operations on each item. This function has four parameters: the index of the previous value, current value, item, and array object. The Base Value of the merge operation is calculated based on this value. For example:

Var values = [5, 6, 7, 8, 9, 10, 11, 12, 13]; values. reduce (function (preitem, item, index, array) {return preitem + item}, 2) // return value 83

The iteration and merge methods described in the above Javascript section are all the content shared by the editor. I hope you can give us a reference and support the help house.

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.