The new method es5 array

es5 new array of methods:
1、 forEach()
Role: through the array, each element to function for processing
语法:arr.forEach(function(item,index,array){........})
item: the elements of the current traversal index: element subscript array: the original array
Advantages: no additional global variables; function with the use of arrows

2、 map()
Role: the implementation process, create an empty array, each processing element to function, the result will be placed into an empty array, eventually returning the new array
语法:let newArr=arr.map(function(item,index,array){...........})

3、filter()
Role: the implementation process, create an empty array, the qualifying elements stored in an empty array, eventually returning the new array
Syntax: let newArr = arr.filter (function (item, index, array) {return condition})

4, some () --- return true or false
Action: whether one or more conditions are satisfied in the array is determined, as long as there is a return to meet true
Syntax: let result = arr.some (function (item, index, array) {return condition})

5, every () --- return true or false
Role: to determine whether all of the array elements to meet the conditions, as long as one does not meet returns false
Syntax: let result = arr.every (function (item, index, array) {return condition})

6, find () --- no returns undefined
Role: through the array, returns the first element qualifying
Syntax: let newArr = arr.find (function (item, index, array) {return condition})
Expansion: find method will return if the judge did not write the condition executed, then the output of all eligible elements, if you write a return, no matter what is written behind the return value, will return the first matching element found

7、findIndex()
Role: iterate, return index of the first element of qualifying
Syntax: let index = arr.findIndex (function (item, index, array) {return condition})
Expansion: findIndex like the find method will return if there is no written return index all eligible elements, if you write a return will return the first matching element subscript

8、 reduce()
Role: to make some kind of before and after an array of two calculations, and then return its value, and continue the calculation, does not change the original list, returns the final result of the calculation, from the beginning of the second array traversal. Some calculation example: a sum, a maximum value, and the like deduplication   
语法:arr.reduce(function(prev,cur,index,arr){...}, init);
parameter:
1, prev will be written, the return value represents the time when the callback function is called, or the initial value of the init, the initial value of the init if not set, direct access to the first item in the array;
2, cur shall be written, represents an array element currently being processed;
  cur's value with an initial value init, and if you set the initial value init cur start value from 0 array, if not set the initial value init, cur start values ​​from the first item in the array;
3, index can write from time to write, represent an indexed array element currently being processed, if provided init value, the index is 0, otherwise the index is 1;
4, arr can write from time to write, represent the original array;
5, init can not write to write the initial value.

9、 includes()
Action: is used to determine whether an array contains a specified value, if so returns true, otherwise false.

Guess you like

Origin www.cnblogs.com/hhmmpp/p/11070411.html