1. Arr.push() adds elements from the back, and the return value is the length of the added array
let arr = [1,2,3,4,5] console.log(arr.push(5)) // 6 console.log(arr) // [1,2,3,4,5,5]
2. Arr.pop() deletes only one element from the back, and the return value is the deleted element
let arr = [1,2,3,4,5] console.log(arr.push(5)) // 6 console.log(arr) // [1,2,3,4,5,5]
3. Arr.shift() deletes elements from the front. Only one element whose return value is deleted can be deleted
let arr = [1,2,3,4,5] console.log(arr.shift()) // 1 console.log(arr) // [2,3,4,5]
4. Arr.unshift() adds elements from the front, and the return value is the length of the added array
let arr = [1,2,3,4,5] console.log(arr.unshift(2)) // 6 console.log(arr) //[2,1,2,3,4,5]
5 arr.splice(i,n) delete the element after I (index value). The return value is the deleted element
param: i index value
n number
let arr = [1,2,3,4,5] console.log(arr.splice(2,2)) //[3,4] console.log(arr) // [1,2,5]
6. Arr.concat() connects two arrays, and the return value is the new array after connection
let arr = [1,2,3,4,5] console.log(arr.concat([1,2])) // [1,2,3,4,5,1,2] console.log(arr) // [1,2,3,4,5]
7 str.split() converts a string into an array
let str = '123456' console.log(str.split('')) // ["1", "2", "3", "4", "5", "6"]
8. Arr.sort() sorts the array, and the return value is the arranged array. By default, it is sorted according to the leftmost number, not the size of the number.
let arr = [2,10,6,1,4,22,3] console.log(arr.sort()) // [1, 10, 2, 22, 3, 4, 6] let arr1 = arr.sort((a, b) =>a - b) console.log(arr1) // [1, 2, 3, 4, 6, 10, 22] let arr2 = arr.sort((a, b) =>b-a) console.log(arr2) // [22, 10, 6, 4, 3, 2, 1]
9. Arr.reverse() inverts the array, and the return value is the inverted array
let arr = [1,2,3,4,5] console.log(arr.reverse()) // [5,4,3,2,1] console.log(arr) // [5,4,3,2,1]
10 arr.slice(start,end) cuts out the array from the index value start to the index value end, excluding the end index value, and the return value is the cut out array
let arr = [1,2,3,4,5] console.log(arr.slice(1,3)) // [2,3] console.log(arr) // [1,2,3,4,5]
11 arr.forEach(callback) traverses the array without return
callback parameter: Value -- the value of the current index
Index -- index
Array -- original array
let arr = [1,2,3,4,5] arr.forEach( (value,index,array)=>{ console.log(`value:${value} index:${index} array:${array}`) }) // value:1 index:0 array:1,2,3,4,5 // value:2 index:1 array:1,2,3,4,5 // value:3 index:2 array:1,2,3,4,5 // value:4 index:3 array:1,2,3,4,5 // value:5 index:4 array:1,2,3,4,5 let arr = [1,2,3,4,5] arr.forEach( (value,index,array)=>{ value = value * 2 console.log(`value:${value} index:${index} array:${array}`) }) console.log(arr) // value:2 index:0 array:1,2,3,4,5 // value:4 index:1 array:1,2,3,4,5 // value:6 index:2 array:1,2,3,4,5 // value:8 index:3 array:1,2,3,4,5 // value:10 index:4 array:1,2,3,4,5 // [1, 2, 3, 4, 5]
12 arr.map(callback) mapping array (traversal array), return returns a new array
callback parameter: Value -- the value of the current index
Index -- index
Array -- original array
let arr = [1,2,3,4,5] arr.map( (value,index,array)=>{ value = value * 2 console.log(`value:${value} index:${index} array:${array}`) }) console.log(arr)
13 arr.filter(callback) filters the array and returns an array that meets the requirements
let arr = [1,2,3,4,5] let arr1 = arr.filter( (i, v) => i < 3) console.log(arr1) // [1, 2]
14 arr.every(callback) determines whether all the elements of the array meet the conditions. If so, true is returned
let arr = [1,2,3,4,5] let arr1 = arr.every( (i, v) => i < 3) console.log(arr1) // false let arr2 = arr.every( (i, v) => i < 10) console.log(arr2) // true
15 arr.some() determines whether one of the elements of the array satisfies the condition. If one satisfies, it returns true
let arr = [1,2,3,4,5] let arr1 = arr.some( (i, v) => i < 3) console.log(arr1) // true let arr2 = arr.some( (i, v) => i > 10) console.log(arr2) // false
16 arr.reduce(callback, initialValue) iterates over all items of the array, accumulators, and combines each value in the array (from left to right), and finally calculates it into one value
Parameter: callback: previousValue required -- the value returned by the callback last time, or the initial value provided (initialValue)
currentValue is required -- the array item currently being processed in the array
Index optional -- the index value of the current array item in the array
Array optional -- original array
initialValue: optional -- initial value
Implementation method: when the callback function is executed for the first time, preValue and curValue can be one value. If initialValue is provided when calling reduce(), the first preValue is equal to initialValue and curValue is equal to the first value in the array; If initialValue is not provided, preValue is equal to the first value in the array
let arr = [0,1,2,3,4] let arr1 = arr.reduce((preValue, curValue) => preValue + curValue ) console.log(arr1) // 10