JS Learning: Summary of Array Methods

Table of contents

I. Introduction

Second, the creation of the array

        1. Array literal notation

        2. Constructor

Three, the array method

        1.join()

        2.push()

        3.pop()

        4.shift()

        5.unshift()

        6.sort()

        7.reverse()

        8.concat()

        9.slice()

        10.splice()

        11.indexOf()

        12.lastIndexOf()

        13.forEach()

        14.map()

        15.filter()

        16.every()

        17.some()

        18.reduce()

        19.reduceRight()


I. Introduction

As one of the basic data types of js Object, arrays are frequently used by developers in daily development. This article summarizes its own tools and methods to facilitate our daily development.

Second, the creation of the array

1. Array literal notation

const arr1 = [] //创建空数组
const arr2 = [0,1,2] //创建包含三项数据且数据为0,1,2的数组

2. Constructor

const arr1 = new Array() //创建一个空数组
const arr2 = new Array(10) //创建包含十项数据且数据都为0的数组
 //当参数不为数字或不止一个时,则将该参数作为元素组成数组
const arr3 = new Array('10')
const arr4 = new Array(1,2)

Three, the array method

There are mainly the following types of prototype methods for arrays:

1.join()

  • Parameter: delimiter, the default value is','
  • Parameter Type:string
  • Function: Use the input parameters as separators to concatenate array elements
  • return type:string
const arr = [1,2,3]
console.log(arr.join()) //1,2,3
console.log(arr.join('-')) //1-2-3

2.push()

  • Parameters: elements to be added to the array
  • Parameter Type:any
  • Function: Add the input parameters to the end of the array and return the length of the array
  • return type:number
const arr = [1,2,3]
const newLen = arr.push(0)
console.log(newLen) //4
console.log(arr) //[1,2,3,0]

3.pop()

  • Parameters: none
  • Parameter Type: None
  • Function: delete and return the element at the end of the array
  • return type:any
const arr = [1,2,3]
const lastItem = arr.pop()
console.log(lastItem) //3
console.log(arr) //[1,2]

4.shift()

  • Parameters: none
  • Parameter Type: None
  • Function: delete and return the first element of the array, if the array is empty, return undefined
  • return type:any
const arr = [1,2,3]
const firstItem = arr.shift()
console.log(firstItem) //1
console.log(arr) //[2,3]

5.unshift()

  • Parameters: elements to be added to the array
  • Parameter Type:any
  • Function: Add the input parameter to the first position of the array and return the length of the array
  • return type:number
const arr = [1,2,3]
const newLen = arr.unshift(0)
console.log(newLen) //4
console.log(arr) //[0,1,2,3]

6.sort()

  • Parameters: Specify the sort order (optional), the default is alphabetical order
  • Parameter Type:function
  • Function: Arrange array elements in ascending order (will change the original array)
  • return type:array
//默认按字母排序时'20'会排在'3'的前面
const arr = [20,1,3]
console.log(arr1.sort()) //[1,20,3]
console.log(arr1) //[1,20,3]
//处理这类问题的解决方法就是给它一个规定排序顺序的函数
const compare = (val1, val2)=>val1-val2
const arr2 = [20,1,3]
console.log(arr2.sort(compare)) //[1,3,20]
console.log(arr2) //[1,3,20]

7.reverse()

  • Parameters: none
  • Parameter Type: None
  • Function: Reverse array elements (will change the original array)
  • return type:array
const arr = [1,3,2]
console.log(arr.sort()) //[2,3,1]
console.log(arr) //[2,3,1]

8.concat()

  • Parameters: the array to be concatenated
  • Parameter Type:array
  • Function: concatenate two or more arrays
  • return type:array
const arr1 = [1,2,3]
const arr2 = [4,5,6]
console.log(arr1.concat(arr2)) //[1,2,3,4,5,6]

9.slice()

  • parameter:
    1. Parameter 1--start position subscript
    2. Parameter 2 (optional) -- the end position subscript, the default value is the end of the array
  • Parameter Type:number
  • Function: Return a new array composed of the specified start position to the end position (excluding the end position element)
  • return type:array
const arr = [1,2,3,4,5]
console.log(arr.slice(1)) //[2,3,4,5]
console.log(arr.slice(1,4)) //[2,3,4]
console.log(arr.slice(1,-1)) //[2,3,4]

10.splice()

  • parameter:
    1. Parameter 1--start position subscript
    2. Parameter 2 (optional) - indicates the number of elements to be removed
    3. Parameter * (arbitrary number of parameters after parameter 2 is optional) - indicates the element to be inserted
  • Parameter type: number, number,any
  • Function: Returns a new array of elements removed from the specified starting position (including the starting position element)
  • return type:array
const arr = [1,2,3,4,5]
const deleteArr = arr.splice(3)
console.log(deleteArr) //[4,5]
console.log(arr) //[1,2,3]
const arr = [1,2,3,4,5]
const deleteArr = arr.splice(3,1)
console.log(deleteArr) //[4]
console.log(arr) //[1,2,3,5]
const arr = [1,2,3,4,5]
const deleteArr = arr.splice(3,1,2)
console.log(deleteArr) //[4]
console.log(arr) //[1,2,3,2,5]

11.indexOf()

  • parameter:
    1. Parameter 1 - the element to be searched
    2. Parameter 2 (optional)--Find the start position subscript
  • Parameter type: any,number
  • Function: Return the subscript of the specified element in the array, if not found, return -1 (search from front to back)
  • return type:number
const arr = [1,2,3,4,5]
console.log(arr.indexOf(3)) //2
console.log(arr.indexOf(3, 2))//2
console.log(arr.indexOf('3'))//-1

12.lastIndexOf()

  • parameter:
    1. Parameter 1 - the element to be searched
    2. Parameter 2 (optional)--Find the start position subscript
  • Parameter type: any,number
  • Function: Return the subscript of the specified element in the array, if not found, return -1 (search from the back to the front)
  • return type:number
const arr = [1,2,3,3,5]
console.log(arr.lastIndexOf(3)) //3
console.log(arr.lastIndexOf(3, 2))//2
console.log(arr.lastIndexOf('3'))//-1

13.forEach()

  • Parameters: the function that each item of the array needs to run
  • Parameter Type:function
  • Function: Run the specified function for each item in the array
  • The function has default parameters: the current array element (required), the subscript of the current element (optional), and the array itself (optional)
  • Return type: None
const arr = [1,2]
arr.forEach((item,index,currentArr)=>{
    console.log(item + " | " + index + " | " + currentArr)
})
//输出结果:
//1 | 0 | [1,2]
//2 | 1 | [1,2]

14.map()

  • Parameters: the function that each item of the array needs to run
  • Parameter Type:function
  • Function: Return a new array composed of the values ​​of each item in the array after running the specified function
  • The function has default parameters: the current array element (required), the subscript of the current element (optional), and the array itself (optional)
  • return type:array
const arr = [1,2]
const newArr = arr.map((item)=>item*item)
console.log(newArr)//[1,4]

15.filter()

  • Parameters: conditional function for filtering
  • Parameter Type:function
  • Function: Returns a new array composed of each eligible function of the array
  • The function has default parameters: the current array element (required), the subscript of the current element (optional), and the array itself (optional)
  • return type:array
const arr = [1,2,3,4,5,6]
const newArr = arr.filter((item)=>item%3===0)
console.log(newArr)//[3,6]

16.every()

  • Parameters: conditional function for judgment
  • Parameter Type:function
  • Function: Return whether each item of the array meets the condition function
  • The function has default parameters: the current array element (required), the subscript of the current element (optional), and the array itself (optional)
  • return type:boolean
const arr = [1,2]
const isLessThan3 = arr.every((item)=>item<3)
console.log(isLessThan3)//true

17.some()

  • Parameters: conditional function for judgment
  • Parameter Type:function
  • Function: Returns whether an element in the array meets the condition function
  • The function has default parameters: the current array element (required), the subscript of the current element (optional), and the array itself (optional)
  • return type:boolean
const arr = [1,2]
const isLessThan3 = arr.some((item)=>item<3)
console.log(isLessThan3)//true

18.reduce()

  • parameter:
    1. Parameter 1--The function that each item of the array needs to run
    2. Parameter 2 -- the initial value passed to the function (optional)
  • Parameter type: function,any
  • Function: Return the cumulative value of each item in the array to run the function result (traverse from front to back)
  • The function has default parameters: initial value (required), current array element (required), current element subscript (optional) and the array itself (optional)
  • return type:any
const arr = [1,2]
const getSum = (total, item)=>total + item
const result1 = arr.reduce(getsum)
console.log(result1)//3
const result2 = arr.reduce(getsum,5)
console.log(result2)//8

19.reduceRight()

  • parameter:
    1. Parameter 1--The function that each item of the array needs to run
    2. Parameter 2 -- the initial value passed to the function (optional)
  • Parameter type: function,any
  • Function: Return the cumulative value of each item of the array running the function result (traversing from the back to the front)
  • The function has default parameters: initial value (required), current array element (required), current element subscript (optional) and the array itself (optional)
  • return type:any
const arr = [1,2]
const getSum = (total, item)=>total + item
const result1 = arr.reduceRight(getsum)
console.log(result1)//3
const result2 = arr.reduceRight(getsum,5)
console.log(result2)//8

Guess you like

Origin blog.csdn.net/qq_52013792/article/details/128819403