Common methods of JS native array operation

1.push()
Syntax: array. Push (data)
Function: append data to the end of the array
Return value: the latest length of the array after appending data

	//Prepare a raw array
    var arr=[100,200,300,400]
    //Output once
    console.log(arr)
    //Execute push method
    var res=arr.push('Additional data')

    console.log(arr)
    console.log(res)


2.pop()
Syntax: array. pop()
Function: delete the last data of the array
Return value: deleted data

	//Prepare a raw array
    var arr=[100,200,300,400]
    //Output once
    console.log(arr)
    //Execute pop method
    var res=arr.pop()

    console.log(arr)
    console.log(res)


3.unshift()
Syntax: array. Unshift (data)
Function: append data to the front of the array
Return value: the latest length of the array after adding data

	//Prepare a raw array
    var arr=[100,200,300,400]
    //Output once
    console.log(arr)
    //Execute the unshift method
    var res=arr.unshift('ha-ha')

    console.log(arr)
    console.log(res)


4.shift()
Syntax: array. shift()
Function: delete the first data of the array
Return value: deleted data

    //Prepare a raw array
    var arr=[100,200,300,400]
    //Output once
    console.log(arr)
    //Execute the shift method
    var res=arr.shift()

    console.log(arr)
    console.log(res)


5.reverse()
Syntax: array. reverse()
Function: invert the array
Return value: array after inversion

    //Prepare a raw array
    var arr=[100,200,300,400]
    //Output once
    console.log(arr)
    //Execute the reverse method
    var res=arr.reverse()

    console.log(arr)
    console.log(res)


6.splice()
Syntax: array. Splice (start index, how many, data to insert)
---------Start index: default is 0
---------How many: the default is 0
---------Data to insert: none by default
Function: delete several data in the array and select whether to insert new data
Return value: returns the deleted data as a new array

    //Prepare a raw array
    var arr=[100,200,300,400]
    //Output once
    console.log(arr)
    //Execute the splice method
    var res=arr.splice(1,1)

    console.log(arr)
    console.log(res)


7.sort()
Syntax:

  • Array. sort()
  • Array. sort(function(a,b){return a-b})
  • Array. sort(function(a,b){return b-a})

Function: sort arrays
Return value: sorted array

sort() ----------- sort by bit

    //Prepare a raw array
    var arr=[4,6,8,2,33,11,22,15,48,9,6,23]
    //Output once
    console.log(arr)
    //Execute sort method
    var res=arr.sort()

    console.log(arr)
    console.log(res)

sort(function(a,b){return a-b}) --------- sort from small to large

    //Prepare a raw array
    var arr=[4,6,8,2,33,11,22,15,48,9,6,23]
    //Output once
    console.log(arr)
    //Execute sort method
    var res=arr.sort(function(a,b){return a-b})

    console.log(arr)
    console.log(res)


sort(function(a,b){return a-b}) --------- sort from large to small

    //Prepare a raw array
    var arr=[4,6,8,2,33,11,22,15,48,9,6,23]
    //Output once
    console.log(arr)
    //Execute sort method
    var res=arr.sort(function(a,b){return b-a})

    console.log(arr)
    console.log(res)


8.join()
Syntax: array. Join (connector)
Function: connect the array into a string with a connector
Return value: connected string

    //Prepare a raw array
    var arr=[4,6,8,2,33]
    //Output once
    console.log(arr)
    //Execute join method
    var res=arr.join('*')

    console.log(arr)
    console.log(res)


9.concat()
Syntax: array. Concat (other arrays)
Function: splice other arrays and arrays together
Return value: spliced new array

    //Prepare a raw array
    var arr=[4,6,8,2,33]
    //Output once
    console.log(arr)
    //Execute concat method
    var res=arr.concat([100,200])

    console.log(arr)
    console.log(res)


10.slice()
Syntax: array. Slice (start index, end index)

  • Start index: default is 0
  • End index: the default is array length

Function: intercept some data in the array
Return value: returns the intercepted data in the form of a new array

    //Prepare a raw array
    var arr=[4,6,8,2,33]
    //Output once
    console.log(arr)
    //Execute slice method
    var res=arr.slice(2,4)

    console.log(arr)
    console.log(res)

ps: before package, not after package

11.indexOf()
Syntax: array. Indexof (data)
Function: find the index position of data in the array
Return value:

  • With this data, the index position of the first occurrence is returned
  • If there is no such data, return * * - 1**

ps: uppercase O of indexOf()

    //Prepare a raw array
    var arr=[4,6,8,2,33,1,6]
    //Output once
    console.log(arr)
    //Execute indexOf method
    var res=arr.indexOf(6)

    console.log(arr)
    console.log(res)


12.forEach()
Syntax: array. forEach(function(item,index,arr) {})
Function: traverse array
Return value: None

    //Prepare a raw array
    var arr=[100,200,300,400]
    //Output once
    console.log(arr)
    //Execute the forEach method
    var res=arr.forEach(function(item,index,arr){
        
        console.log(item)
        console.log(index)
        console.log(arr)
        console.log('-----------------')
    })


13.map()
Syntax: array. map(function(item,index,arr) {})
Function: mapping array
Return value: new array after mapping

    //Prepare a raw array
    var arr=[100,200,300,400]
    //Output once
    console.log(arr)
    //Execute map method
    var res=arr.map(function(item,index,arr){
    	//Write the mapping condition as return
        return item*10
    })
    console.log(arr)
    console.log(res)


14.filter()
Syntax: array. filter(function(item,index,arr) {})
Function: filter array
Return value: new array after filtering

    //Prepare a raw array
    var arr=[100,200,300,400]
    //Output once
    console.log(arr)
    //Execute filter method
    var res=arr.filter(function(item,index,arr){
        //Write the mapping condition as return
        return item>=300
    })
    console.log(arr)
    console.log(res)


15.every()
Syntax: array. every(function(item,index,arr) {})
Function: judge whether each item of the array meets the conditions
Return value: a Boolean value (false/true)

    //Prepare a raw array
    var arr=[100,200,300,400]
    //Output once
    console.log(arr)
    //Execute every method
    var res=arr.every(function(item,index,arr){
        //Write the mapping condition as return
        return item>=300
    })
    console.log(arr)
    console.log(res)


16.some()
Syntax: array. some(function(item,index,arr) {})
Function: judge whether all items in the array meet the conditions
Return value: a Boolean value (false/true)

    //Prepare a raw array
    var arr=[100,200,300,400]
    //Output once
    console.log(arr)
    //Execute some method
    var res=arr.some(function(item,index,arr){
        //Write the mapping condition as return
        return item>=300
    })
    console.log(arr)
    console.log(res)


The above is the method of array operation commonly used in js. It is easy to understand, use and remember

Tags: Javascript

Posted on Fri, 26 Nov 2021 12:20:45 -0500 by mxdan