3. Modern javascript array of topics

An array of topics

  Operators Expand

    ... use the symbol, the array can be "expanded."

    Expanded array of magical ... 

    eg:

      // instead of apply

      const foo = [1, 2, 3]

      const bar = [4, 5, 6]

      foo.push (bar ...) // corresponds foo.push (4, 5, 6)

      // false array to a true array

      var nodeList = document.querySelectorAll('div')

      var earlArray = [...nodeList]

      // concat complex operations like in place of, the array structure

      var parts = ['shoulders', 'knees'];

      var lyrics = ['head', ...parts, 'and', 'toes'];

  find and findIndex

    Recommendation: Do not use a for loop or forEach or filter to find an element of the array!

    Highlights:

      The difference findIndex and indexOf

        indexOf only incoming value

        eg: find within the index element of the array is equal to 2

          const arr = [1,2,3,4,5];

          const ret = arr.indexOf(2);

        findIndex can pass expressions

        eg: Find the index value is greater than a first element 2

          const arr = [1,2,3,4,5];

          const ret = arr.findIndex(el=>{el>2});

    expand:

      a map is used to map the array of another array

        eg: all of the values ​​in the array is multiplied by 2 

          const arr = [1,2,3,4,5];

          const arr2 = arr.map(el=>{return el * 2})

      to reduce by a certain operation merge array elements

        eg: all the elements in the array is multiplied by

          const arr = [1,2,3,4,5];

          const ret = arr.reduce((prev, el)=>{return el * prev}, 1)

      filter used to filter array elements satisfying the conditions

        eg: greater than 2 within the array element screened

          const arr = [1,2,3,4,5];

          const arr2 = arr.filter(el=>{return el > 2})

      some used to determine the array element satisfies certain conditions have not (yet at least satisfies returns true not satisfy a return false)

        eg: determining whether there is the element of the array is greater than a 2

          const arr = [1,2,3,4,5];

          const ret = arr.some(el=>{return el > 2})

      for every element in the array is determined not satisfy certain conditions (all not met must meet satisfy returns true return false)

        eg: the determination of all array elements is greater than 2

          const arr = [1,2,3,4,5];

          const ret = arr.every(el=>{return el > 2})

      forEach used for traversal (unless you are using forEach to write shorter code, do not use)

      find and findIndex used to find qualified elements

        eg: Find the index value is greater than the element 5

          const arr = [2,4,6,8,10];

          const value = arr.find (el => {return el> 5}); // Find the first value is greater than five

          const index = arr.findIndex (el => {return el> 5}); // Find the first index is larger than five

   includes

    includes an array is not used to determine a value comprising

    Points

      And includes the difference between the indexOf

        includes 返回 true or false

        indexOf Returns the index can not find the find is returned or -1

        eg: 2 determines whether or not included

          const arr = [1,2,3,4,5];

          const ret1 = !arr.includes(2);

          const ret2 = (arr.indexOf(2) === -1);

 

Function topics

  Operators Expand

    Parameter expansion

    function fn(...params){

      

    }

Guess you like

Origin www.cnblogs.com/zonehoo/p/11505503.html