ES6 to add a lot of methods to the array, I first summarize the previous method:
1.array.isarray (): Detecting arrays
var arr = ['a','b','C' ]var'abc'console.log (Array.isarray (arr)) // trueconsole.log (Array.isarray (str))//false
2.push (): Receive any number of parameters and add them to the end of the array one by one
var arr = [ " a ", " b ", " c var item = Arr.push ( '
3.pop (): Removes the last item from the end of the array
var arr = [' A ', ' B ', ' C ']var item = arr.pop () console.log (item)//C Console.log (arr)//["A", "B"]
4.shift (): Removes the first item of an array and returns the item
var arr = [' A ', ' B ', ' C ']var item = arr.shift () console.log (item)// aconsole.log (arr)//
5.unshift (): Ability to add any item to the front of the array and return the new array length
var arr = [' A ', ' B ', ' C ']var item = arr.unshift (' d ', ' e ') console.log (item)// 5console.log (arr)//["D", "E", "A", "B", "C"]
6.reverse (): Reverses the order of the data items, noting that it has changed the original array
var arr = [2,4,6,3,1]console.log (Arr.reverse ())//[1, 3, 6, 4, 2]Console.log ( ARR)//[1, 3, 6, 4, 2]
7.sort (): Can receive a comparison function as a parameter, the comparison function receives two parameters, if the first parameter should be located before the second parameter will return a negative number, two parameters equal to return 0, if the first parameter should be located after the second parameter will return a positive number
var arr = [2,4,6,3,1]arr.sort (function(b) { return A- B}) Console.log (arr)//[1, 2, 3, 4, 6]
8.concat (): Creates a new array based on all the items in the current array, feeling that the string is manipulated in a similar way
var arr1 = [' A ', ' B ', ' C ']var arr2 = [' d ', ' e ', ' F ']var arr = Arr1.concat ( ARR2) Console.log (arr)//["A", "B", "C", "D", "E", "F"]Console.log (arr1)//["A", "B "," C "]
9.slice (): Intercepts the original array, constructs a new array, and the original array remains unchanged
var arr = [' A ', ' B ', ' C ', ' d ', ' E ']var arr1 = Arr.slice (2)var arr2 = Arr.slice (2,3) console.log (arr1) // ["C", "D", "E"]console.log (ARR2)//["C"]
10.splice (): This is probably the most powerful array method, add, delete, change, Operation Almighty
(1) Increase, you can insert any number of items to the specified location, just provide three parameters, starting position, 0 (number of items to delete) and the item to be inserted
var arr = [' A ', ' B ', ' C ']arr.splice (1,0, ' d ', ' e ') console.log (arr)// ["A", "D", "E", "B", "C"]
(2) by deleting, provide two parameters, the position of the first item to be deleted and the number of items to be deleted
var arr = [' A ', ' B ', ' C ', ' d ']arr.splice (console.log) (arr)//["A", "D"]
(3) Change, combined with the above two ways, the first to delete and then increase is to change
var arr = [' A ', ' B ', ' C ', ' d ', ' E ']arr.splice (, ' f ', ' G ') Console.log (arr)//[ "A", "F", "G", "D", "E"]
11.indexOf (), lastIndexOf () They all receive two parameters, the index of the item to find and the location of the lookup starting point (optional), indexOf () to look backwards from the beginning, lastIndexOf looking forward from the end
var arr = [' A ', ' B ', ' C ', ' d ', ' C ', ' E ']console.log (Arr.indexof (' C ', 3))//4 Console.log (Arr.lastindexof (' C ', 3))//2
12.every (): Runs the given function for each item in the array, and returns True if the function returns true for each item
var num = [1,2,3,4,5,6]var everyresult = Num.every (function(item, Index, Array) { return (item > 0)}) Console.log (everyresult)//True
13.filter (): Each item in an array runs the given function, and returns the list of items that the function returns True
var num = [1,2,3,4,5,6]var filterresult = Num.filter (function(item, Index, Array) { return (item > 3)}) Console.log (filterresult)//[4, 5, 6]
14.forEach (): Runs the given function for each item in the array, this method does not return a value
var num = [1,2,3,4,5,6]num.foreach (function(item, index, array) { Console.log (item)//1,2,3,4,5,6})
15.map (): Each item in an array runs the given function, and returns a list of the results of each function call
var num = [1,2,3,4,5,6]var mapresult = Num.map (function(item, index, array) { return Item * 2}) Console.log (mapresult)//[2, 4, 6, 8, ten, each]
16.some (): Runs the given function for each item in the array and returns TRUE if the function returns true for either item
var num = [1,2,3,4,5,6]var someresult = Num.some (function(item, index, array) { return item > 5}) Console.log (someresult)//true
17.reduce (), Reduceright (): Iterates through all the items of an algebraic group, and then constructs a final returned value, reduce () starting with the first item, Reduceright () starting with the last item
var num = [1,2,3,4,5,6]var reduceresult = Num.reduce (function(prev, cur, index , array) { return prev + cur}) console.log (reduceresult)//
ES6 Small Experiment-review arrays