JS common recording method of operating an array

1.map () method

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

2.forEach()

  • Each element of the array to call a method, it will change the original array
  • exp:
   var arr = [ '10','20','30','40','50' ]
   var num1 = arr.forEach( ( el,index )=>{
       el = el*2
       // 此时arr变为[ '20','40','60','80','100' ]
   } )

3.filter()

  • Each match in the array, will meet the condition that a new array as a return, will not change the original array
    var arr = [10,20,30,40,50]
    var num1 = arr.filter( ( item,index )=>{
        return item > 30
    } )
    // 此时num1为[40,50]

4.every()

  • All elements of an array judge returns a Boolean value returned if all elements are met is true, false otherwise
    var arr = [10,20,30,40,50]
    var num1 = arr.every( ( item,index )=>{
        return item > 30
    } )
    // 此时num1为false,因为不是所有有数字都比30小

5.some()

  • Elements in the array is determined, if there is one element in the array satisfies the conditions are true or false otherwise
    var arr = [10,20,30,40,50]
    var num1 = arr.some( ( item,index )=>{
        return item > 40
    } )
    // 此时num1为true因为里面有元素比40大

6.reduce()

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

    var arr = [10,20,30,40,50]
    var num1 = arr.reduce( ( item,index )=>{
        return item + index
    } )
    //  此时num1为150,因为是将该数组中每一项进行叠加处理

7.push()

  • Add after the last item in the array in an array,
  • It 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 the last item of the array
  • It will change the original array
  • The return value is one that is deleted
    var arr = [10,20,30,40,50]
    var num1 = arr.pop()
    // num1 = 50 返回值是删除的那项

9.shift()

  • The first item to delete the array
  • It will change the original array
  • The return value is one that is deleted
    var arr = [10,20,30,40,50]
    var num1 = arr.shift()
    // num1 = 10 返回值是删除的那项

10.unshift()

  • In front of the first term plus an array of one or more arrays
  • It 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()

  • Determining 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()

  • The two arrays splicing
  • It does not change the original array
  • The return value is a new array
    var arr = [10,20,30,40,50]
    var str = [70,80]
    var num1 = arr.concat( str )
    // 此时num1 = [10,20,30,40,50,70,80]
    // es6数组简单拼接方法:
    [...arr,...str]

13.toString()

  • Into a string array, may be implemented splicing
  • It does not change the original array
  • The return value is converted into an array of strings
    var arr = [10,20,30,40,50]
    var num1 = arr.toString(  )

14.join()

  • The array into a string, a simple transformation
  • It does not change the original array
  • The return value is converted into an array of strings
    var arr = [10,20,30,40,50]
    var num1 = arr.join(  )

15.splice()

  • Additions and deletions to achieve an array of
  • We will modify the original array
  • The return value is one that has been modified
    var arr = [10,20,30,40,50]
    var num1 = arr.splice( 0,1,60 )
    // 此时num1 = 【10】,arr = [60,20,30,40,50]

16 Array.from(arrayLike,mapFn,thisArg)

  • Creates a new array instance of a class from an array or object iteration
  • arrayLike: pseudo like into an array or an array of objects iterables
  • The callback function will be executed each element mapFn (optional parameter) new array
  • thisArg (optional) executes mapFn this callback objects
  • The return value is a new instance of an array of
    Array.from(arrayLike, mapFn, thisArg)
Released two original articles · won praise 11 · views 204

Guess you like

Origin blog.csdn.net/qq_42698109/article/details/104680276