Record common array operation methods of JS

1.map() method

  • Calling a method for each element in the array does not change the original array
  • exp:
   var arr = [ '10','20','30','40','50' ]
   var num1 = arr.map( ( el,index )=>{
       el = el*2
       // arr or ['10 ',' 20 ',' 30 ',' 40 ',' 50 ']
   } )

2.forEach()

  • Calling a method for each element in the array will change the original array
  • exp:
   var arr = [ '10','20','30','40','50' ]
   var num1 = arr.forEach( ( el,index )=>{
       el = el*2
       // At this time, arr changes to ['20 ',' 40 ',' 60 ',' 80 ',' 100 ']
   } )

3.filter()

  • Match each item in the array, return the item that meets the condition as a new array, without changing the original array
    var arr = [10,20,30,40,50]
    var num1 = arr.filter( ( item,index )=>{
        return item > 30
    } )
    // Now num1 is [40,50]

4.every()

  • Judge all elements in the array and return a Boolean value. If all elements are satisfied, return true. Otherwise, return false
    var arr = [10,20,30,40,50]
    var num1 = arr.every( ( item,index )=>{
        return item > 30
    } )
    // Now num1 is false, because not all the numbers are smaller than 30

5.some()

  • Judge the elements in the array. If one element in the array meets the conditions, it returns true. Otherwise, it returns false
    var arr = [10,20,30,40,50]
    var num1 = arr.some( ( item,index )=>{
        return item > 40
    } )
    // Now num1 is true because there are elements larger than 40

6.reduce()

*Call the function on all elements in the array, and the return value is the final result

    var arr = [10,20,30,40,50]
    var num1 = arr.reduce( ( item,index )=>{
        return item + index
    } )
    //  At this time, num1 is 150, because each item in the array is stacked

7.push()

  • Add an array after the last item of the array,
  • Will change the original array
  • The return value is the length of the new array
    var arr = [10,20,30,40,50]
    var num1 = arr.push( 60 )
    // num1 = 6,

8.pop()

  • Delete last entry of array
  • Will change the original array
  • The item whose return value is deleted
    var arr = [10,20,30,40,50]
    var num1 = arr.pop()
    // num1 = 50 the item whose return value is deleted

9.shift()

  • Delete first entry of array
  • Will change the original array
  • The item whose return value is deleted
    var arr = [10,20,30,40,50]
    var num1 = arr.shift()
    // num1 = 10 the item whose return value is deleted

10.unshift()

  • Add one or more arrays before the first item of the array
  • Will change the original array
  • The return value is the length of the new array
    var arr = [10,20,30,40,50]
    var num1 = arr.unshift( 60 )
    // num1 = 6

11.isArray()

  • Determine whether an object is an array
  • The return value is a Boolean value
    var arr = [10,20,30,40,50]
    var num1 = Array.isArray( arr )
    // num1 = true 

12.concat()

  • Splicing two arrays
  • The original array will not be changed
  • The return value is a new array
    var arr = [10,20,30,40,50]
    var str = [70,80]
    var num1 = arr.concat( str )
    // Now num1 = [10,20,30,40,50,70,80]
    // Simple splicing method of es6 array:
    [...arr,...str]

13.toString()

  • The array can be converted into a string to realize splicing
  • The original array will not be changed
  • The return value is a string converted from an array
    var arr = [10,20,30,40,50]
    var num1 = arr.toString(  )

14.join()

  • Convert array to string, simple conversion
  • The original array will not be changed
  • The return value is a string converted from an array
    var arr = [10,20,30,40,50]
    var num1 = arr.join(  )

15.splice()

  • Add, delete and modify arrays
  • The original array will be modified
  • The return value is the modified item
    var arr = [10,20,30,40,50]
    var num1 = arr.splice( 0,1,60 )
    // Now num1 = [10], arr = [60,20,30,40,50]

16 Array.from(arrayLike,mapFn,thisArg)

  • Create a new array instance from an array of classes or iteratable objects
  • arrayLike: pseudo array object or iteratable object that you want to convert to an array
  • Mapfn (optional parameter) callback function to be executed for each element in the new array
  • this object when thisarg (optional parameter) executes the callback function mapFn
  • The return value is a new array instance
    Array.from(arrayLike, mapFn, thisArg)
Published 2 original articles · praised 6 · visited 33
Private letter follow

Posted on Thu, 05 Mar 2020 02:35:41 -0800 by cupboy