Array method

Array method

1. The Array.isArray() method returns a boolean value indicating whether the parameter is an array

the let ARR = [ . 1 , 2 ] typeof ARR //'Object' the Array .isArray (ARR) //to true copy the code

2. The valueOf() method, the valueOf method of the array returns the array itself

let arr = [ 1 , 2 ] arr.valueOf () //[. 1, 2] copy the code

3. The toString() method, the toString method of the array returns the string form of the array

let arr = [ 1 , 2 ] arr.toString () //'1,2' duplicated code

4. The push() method is used to add one or more elements to the end of the array and return the length of the array after adding new elements. This method will change the original array

let arr = [] arr.push ( . 1 ) //. 1 arr.push ( 'A' ) //2 arr.push ( to true , {}) //. 4 ARR //[. 1, 'A', to true, {}] Copy the code

5. The pop() method is used to delete the last element of the array and return that element. This method will change the original array

let arr = [ 1 , 2 , 3 ] arr.pop() //3 arr //[1, 2] [] .pop () //undefined empty array using this method is not given, but returns undefined duplicated code

6. The shift() method is used to delete the first element of the array and return the element. This method will change the original array

let arr = [ 'a' , 'b' , 'c' ] arr.shift() //'a' arr//[ 'B', 'C'] duplicated code

7. The unshift() method is used to add an element to the first position of the array and returns the length of the array after adding the new element. This method will change the original array

let arr = [ 1 , 2 ] arr.unshift( 0 ) //3 arr //[0, 1, 2] //unshift() method can accept multiple parameters, these parameters will be added to the head of the target array let arr = [ 'c' , 'd' ] arr.unshift( 'a' , 'b' ) //4 arr //['a','b','c','d'] duplicated code

8. The join() method uses the specified parameter as the separator to connect all the array members into a string and returns. If no parameter is provided, it is separated by a comma by default

let arr = [ 1 , 2 , 3 , 4 ] arr.join( '' ) //'1 2 3 4'arr.join( '|' ) //'1 | 2 | 3 | 4'arr.join() //'1,2,3,4' //If the array member is undefined or null or empty, it will be converted to an empty string [ undefined , null ].join( '#' ) //'#' [ 'a' ,, 'b' ].join( ' -' ) //'a--b' //Through the call method, this method can also be used for strings or array-like objects Array .prototype.join.call( 'hello' , '-' ) //'hell-o' let obj = { 0 : 'a ' , . 1 : ' B ' , length : 2 }; the Array .prototype.join.call (obj, ' - ' ) //' A-B ' duplicated code

9. The concat() method is used to merge multiple arrays. It adds the members of the new array to the back of the original array members, and then returns a new array with the original array unchanged

[ 1 ].concat([ 2 ]) //[1, 2] [].concat({ a : 1 }, { b : 2 }) //[{ a: 1 }, {b: 2 }] [ 1 , 2 , 3 ].concat( 4 , 5 , 6 ) //[1, 2, 3, 4, 5, 6] //concat returns a shallow copy of the current array let obj = { a : 1 }; let oldArray = [obj]; let newArray = oldArray.concat(); obj.a = 2 ; newArray[ 0 ].a // 2Copy code

10. The reverse() method is used to reverse the array elements and return the changed array. This method will change the original array

let arr = [ 'a' , 'b' , 'c' ]; arr.reverse () //[ "C", "B", "A"] ARR //[ "C", "B", "A"] Copy Code

11. The slice() method is used to extract a part of the target array and return a new array with the original array unchanged

//start is the starting position, end is the ending position (but the element itself at this position is not included), //if the end parameter is omitted, it will always return to the last member of the original array arr.slice(start, end) let arr = [ 'a' , 'b' , 'c' ] arr.slice( 0 ) //["a", "b", "c"] arr.slice( 1 ) //["b", "c"] arr.slice( 1 , 2 ) //[" b"] arr.slice( 2 , 6 ) //["c"] //If the parameter of the slice() method is negative, it means the position of the reciprocal calculation arr.slice(- 2 ) //["b", "c"] arr.slice(- 2 , -1 ) //[" b"] //If the first parameter is greater than or equal to the length of the array, or the second parameter is less than the first parameter, an empty array is returned arr.slice( 4 ) //[] arr.slice( 2 , 1 ) //[] Copy Code

An important application of the slice() method is to convert an array-like object into a real array

Array .prototype.slice.call({ 0 : 'a' , 1 : 'b' , length : 2 }) //["a", "b"] Array .prototype.slice.call( document .querySelectorAll( " div " )) Array .prototype.slice.call ( arguments The ) copy the code

12. The splice() method is used to delete part of the members of the original array, and can add new members at the deleted position, the return value is the deleted element, this method will change the original array

arr.splice(start, count, addEle1, addEle2) //The start parameter is the starting position of the deletion (starting from 0), count is the number of deletions, and the following parameter represents the new element inserted into the array let arr = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ] arr.splice( 4 , 2 ) //["e", "f"] arr //["a", "b", "c", "d"] let arr1 = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ]; arr1.splice( 4 , 2 , 1 , 2 ) //["e", "f"] arr1 //["a", "b", "c", "d", 1, 2] //If the starting position is negative, it means starting from the inverse position let arr2 = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ]; arr2.splice(- 4 , 2 ) //["c", "d"] arr2 //["a", "b", "e", "f"] //If you simply insert elements, the second parameter of the splice method can be set to 0 let arr3 = [ 1 , 1 , 1 ]; arr3.splice ( . 1 , 0 , 2 ) //[] ARR3 //[. 1, 2,. 1,. 1] Copy the code

13. The sort() method sorts the members of the array, the default is to sort according to the dictionary order, the original array will change after sorting

[ 'd' , 'c' , 'b' , 'a' ].sort() //['a','b','c','d'] [ 11 , 101 ].sort() //[101, 11] //If you want the sort method to sort in a custom way, you can pass in a function as a parameter [ 10111 , 1101 , 111 ].sort( function ( a, b ){ return a - b; }) //[111, 1101, 10111] copy the code

14. map()

let numbers = [1, 2, 3] numbers.map(function (n) { return n + 1; }) //[2, 3, 4] //The map() method accepts a function as a parameter, and three parameters are passed in when the function is called: the current member, the current position, and the array itself [1, 2, 3].map(function(elem, index, arr) { return elem * index }) //[0, 2, 6] //map() this var arr = ['a', 'b', 'c']; [ 1 ,2].map(function (e) { return this[e]; }, arr) //['b','c'] //The map() method will not skip undefined and null, but will skip empty spaces let f = function ( n ) { return'a ' }; [ 1 , undefined , 2 ].map(f) //["a", "a", "a"] [ 1 , null , 2 ].map(f) //["a", "a", "A"] [ . 1 ,, 2 ] .map (F) //[ "A",, "A"] copy Code

15. The forEach() method, which is very similar to the map() method, also executes the parameter function sequentially on all members of the array. However, the forEach() method does not return a value and is only used to manipulate data.

function log ( element, index, array ) { console .log( '[' + index + '] = ' + element); } [ 2 , 5 , 9 ].forEach(log); //[0] = 2 //[1] = 5 //[2] = 9 //The forEach() method cannot interrupt execution, it will always traverse all members. If you want to meet a certain condition, interrupt the traversal, use a for loop let arr = [ 1 , 2 , 3 ]; for ( let i = 0 ; i <arr.length; i++) { if (arr[i] = == 2 ) break ; console .log(arr[i]); } // 1Copy code

16. The filter() method is used to filter array members. The members that meet the conditions form a new array and return. Its parameter is a function. All array members execute the function in turn. The members whose return result is true form a new array and return. This method will not change the original array

[ 1 , 2 , 3 , 4 , 5 ].filter( function ( ele ) { return ele> 3 }) //[4, 5] //The parameter function of the filter() method can accept three parameters: the current member, the current position and the entire array [ 1 , 2 , 3 , 4 , 5 ].filter( function ( elem, index, arr ) { return index% 2 === 0 }) //[1, 3, 5] Copy code

17. The some() method, as long as the return value of a member is true, the return value of the entire some method is true, otherwise it returns false

let arr = [ 1 , 2 , 3 , 4 , 5 ] arr.some ( function ( elem, index, ARR ) { return elem> = . 3 }) //to true copy the code

18. The every() method is that the return value of all members is true, and the entire every method returns true, otherwise it returns false

let arr = [ 1 , 2 , 3 , 4 , 5 ] arr.every ( function ( elem, index, ARR ) { return elem> = . 3 }) //to false copy the code

19. The reduce() method and reduceRight() method process each member of the array in turn, and finally accumulate to a value. The difference is that reduce is processed from left to right, reduceRight is processed from right to left, and the others are exactly the same.

[ 1 , 2 , 3 , 4 , 5 ].reduce( function ( prev, cur ) { console .log(prev, cur) return prev + cur }) //12 //33 //64 //105 //Last Result: 15 copy the code