js array method collection

1. Manipulate items in an array

1,push&pop

let arr = [1,2,3,4,5]
arr.push(6)    //arr becomes [1,2,3,4,5,6] and the return value is 6
arr.pop()       //arr becomes [1,2,3,4,5] and the return value is 6

2,shift&unshift

let arr = [1,2,3,4,5]
arr.shift()    //arr becomes [2,3,4,5] and the return value is 1
arr.unshift(8)       //arr becomes [6,2,3,4,5], the return value is 5 (array length)

3,concat

let arr = [1,2,3,4,5]
let newarr = arr.concat(6,[7,8],[[9,10]])  //does not change the original array
console.log(newarr) //[1, 2, 3, 4, 5, 6, 7, 8, [9,10]] // will expand a layer of array

4,slice&splice
The slice() method accepts one or two parameters, indicating the start and end positions of the items to be returned, without changing the original array, and returning all items from the start to the end position, excluding the end item, if the parameter is negative, an array is used Add this number to the length to determine the position.

let arr = [1,2,3,4,5]
let newarr = arr.slice(1,3)
console.log(newarr)   //[2,3]

The splice() method can delete, insert, and replace items in an array
Two parameters are provided when deleting, the position of the first item to be deleted and the number of items to be deleted, which will change the original array and return the deleted item.

let arr = [1,2,3,4,5]
let newarr = arr.splice(1,3)
console.log(arr)     //[1,5]
console.log(newarr)   //[2,3,4]

Three parameters are provided when inserting, the starting position, 0 (the number of items to delete), and the item to be inserted. will change the original array, returning the deleted item.

let arr = [1,2,3,4,5]
arr.splice(1,0,6,7,8)
console.log(arr)     //[1, 6, 7, 8, 2, 3, 4, 5]

Three parameters are provided when replacing, the starting position, the number of items to be deleted, and the items to be inserted. will change the original array, returning the deleted item.

let arr = [1,2,3,4,5]
let newarr  =arr.splice(1,3,6,7,8)
console.log(arr)     //[1, 6, 7, 8, 5]
console.log(newarr)  //[2, 3, 4]

5,indexOf&lastIndexOf
These two methods take two arguments, the item to look for and an optional index at which to start the lookup.

let arr = [1,2,3,4,2]
let index  =arr.indexOf(2)
let lastindex = arr.lastIndexOf(2)
console.log(index)     //1
console.log(lastindex) //4

6,sort&reverse

The sort() method by default calls the toString() method on each item in the array, and then compares the resulting strings.

let arr = [13,2,6,8,4]
arr.sort()
console.log(arr)    //[13, 2, 4, 6, 8]

Since the default is to sort by string, if we want to sort by value, we can pass in a comparison function as a parameter, and the return positive number in the comparison function needs to be exchanged.

let arr = [13,2,6,8,4]
arr.sort((a,b)=>{return a-b})
console.log(arr)    //[2, 4, 6, 8, 13]

The reverse() method reverses the array order

let arr = [13,2,6,8,4]
arr.reverse()
console.log(arr)    //[4, 8, 6, 2, 13]

Second, the traversal operation method

1,forEach
Run the callback function for each item in the array, there is no return value, change the original array, and cannot be terminated (break, return fail).

let arr = [13,2,6,8,4]
arr.forEach((item,index,array)=>{
    array[index] = item * 2
})
console.log(arr)    //[26, 4, 12, 16, 8]

2,map
Run the callback function for each item in the array and return the new array after the operation, without changing the original array

let arr = [13,2,6,8,4]
let newarr = arr.map((item,index,array)=>{
    return item * 2
})
console.log(newarr)    //[26, 4, 12, 16, 8]

3,filter
Run the callback function for each item in the array, and return an array of items whose function returns true, without changing the original array

let arr = [13,2,6,8,4]
let newarr = arr.filter((item,index,array)=>{
    return item > 2
})
console.log(newarr)    //[13, 6, 8, 4]

4,reduce
Receive two parameters, namely the callback function and the initial value. The callback function receives four parameters, and runs the callback function for each item in the array. The value returned by this function will be passed as the first parameter to the next item, so as to achieve The effect of accumulation returns the accumulated result without changing the original array.

let arr = [13,2,6,8,4]
let res = arr.reduce((prev,cur,index,array)=>{
    return prev+cur
},0)
console.log(res)    //33

5,every&some
Run the callback function for each item in the array, every is the function that returns true for each item and returns true, some is the function that returns true for any item and returns true.

let arr = [13,2,6,8,4]
let res = arr.every((item,index,array)=>{
    return item > 2
})
console.log(res)    //false
let arr = [13,2,6,8,4]
let res = arr.some((item,index,array)=>{
    return item > 2
})
console.log(res)    //true

Tags: Javascript Front-end programming language

Posted by DarkHavn on Wed, 04 May 2022 14:47:25 +0300