Home  >  Article  >  Web Front-end  >  Detailed interpretation of iteration and merging methods in Javascript (graphic tutorial)

Detailed interpretation of iteration and merging methods in Javascript (graphic tutorial)

亚连
亚连Original
2018-05-21 09:41:251196browse

Below I will bring you a detailed explanation of the iteration and merging methods in Javascript. Let me share it with you now and give it as a reference for everyone.

Iteration method

The iteration method in Javascript personally feels that it is particularly important. In many cases, there will be actual needs. JavaScript provides 5 There are an iteration method for us to operate, they are:

every() applies the given function to each item in the array, if each item returns true, then true will be returned

filter() applies the given function to each item in the array, composes the items that return true into a new array and returns

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() Each item in the array uses the given function. If an item in the array returns true, then it returns true

Among the five methods above, they all accept two parameters: Execute the function, that is, you need A function that operates on each item. This function takes 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. Such as:

var values = [5,6,7,8,9,10,11,12,13];
 
function actionfunc(item, index, array){console.log(this)};
 
values.every(actionfunc,document); //这里会向控制台输出6次document对象

Merge method

In addition to the iterative method, javascript Two merge methods are also provided. Merge is archive merge. Like the name, these methods will use the given function to iterate each item in the array and then return a total value. The two merging methods are:

reduce() applies the given function to each item in the array from the first to the last item in the array, and then returns a The sum of the results of running the given function over all items in the array.

reduceRight() applies the given function in reverse order from the last item in the array to the first, and then returns the sum of the results of running the given function on all items in the array.

The above two methods accept two parameters: execution function, which is a function that needs to operate on each item. This function has four parameters: previous value, current value, index of the item, array The object itself. The base value of merging. The calculation of merging will be 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) //返回数值83

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed explanation of javascript prototype prototype (basic course)

The second parameter of javascript replace() is Function parameter usage (detailed explanation for everyone)

JavaScript output display content (basic tutorial)

The above is the detailed content of Detailed interpretation of iteration and merging methods in Javascript (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn