Iterative methods
Iterative methods are particularly important in JavaScript, and in many cases there are actual requirements, JavaScript provides 5 iterative methods for us to operate, respectively:
Every () each item in the array uses the given function, and returns TRUE if each entry returns true
Each item in the array of filter () uses the given function to form a new array of items that return true and return
Each of the items in the ForEach () array uses the given function, but there is no return value
Map () Each item in the array uses the given function and returns the result of each function call to form a new array
Same () each item in the array uses the given function and returns True if one of the entries returns true
Of the 5 methods above, they all accept two parameters: the function, the function that needs to operate on each item, which has three parameters: the value of the array item, the position of the item in the array, the array object itself. The given scope, given a scope, affects the This object for 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); This will output the Document object to the console 6 times
Merge method
In addition to the iterative approach, JavaScript also provides two merged methods, merging is the filing of merges, which, like names, take advantage of each item in the given function's iterated algebra group and 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, and then returns the sum of the results of the given function for all the entries in a group of arrays.
Reduceright () in an array, the item runs from the last to the first reverse, using the given function, and then returns the sum of the results of the given function running on all the entries of the logarithm group.
The above two methods accept two parameters: the function, which is the one that needs to operate on each item, has four parameters: the previous value, the current value, the index of the item, and the array object itself. The merged base value, which is calculated on the basis of this value. Such as:
var values = [5, 6, 7, 8, 9, ten, one, each];
Values.reduce (function (preitem,item,index,array) {return preitem+item},2)//Back Value 83
Above this javascript in the iterative, merging method is small series to share the whole content of everyone, hope to give you a reference, but also hope that we support cloud habitat community.