8. expanding array

Expanding array

A Extended operator

  1. Meaning: Extended operator is three dots (...), the role is to convert the array is a sequence of parameters separated by commas

    console.log(1,...[2,3,4],5);
    
    //等价于
    console.log(1,2,3,4,5);
  2. usage:

    • The operator is mainly used to call the function
    • Normal extended operator function parameters may be used in combination
    • Operators can be placed behind the extension expression
    • If the operator is empty array extension, it has no effect
    function func(a,b,c,d,e){}
    //调用
    func(1,...[2,3],4,...[5])
    
    func(1,...(x > 0 ? [2,3,4,5] : [6,7,8,9]))
  3. Application of extended operator

    • Copy the array
    const a1 = [2,3,4];
    
    //如下两种方式都可以实现数组的复制
    const b1 = [...a1];
    const [...b2] = a1;
    • Array merge
    const a1 = [1,2,3];
    const a2 = [4,5,6];
    
    const b1 = [...a1,...a2];
    • Combined with the destructuring assignment, generating an array
    let [a,...list] = [1,2,3,4,5,6];
    • If you extend the array assignment operator is used only on the last one, otherwise an error
    let [a,...nums,c] = [1,2,3,4,5];     //报错
    • The string to an array
    [...'hello']     //['h','e','l','l','o']

II. Examples of the method of the array

1. Array.from()

  1. Array.from for the two types of objects into real array

    • Similar arrays of objects
    • You can traverse objects
  2. Any object has a length property, can be converted to an array by Array.from

  3. If the parameter is an array, Array.form returns a new array of identical

  4. If the parameter is a string, Array.from method will string into character array

  5. Array.from can also accept a second parameter, map array similar to the method used to perform the processing for each element, the values ​​of the array returned into the process

    Array.from(arrList,x => x * x);
    //等价于
    Array.from(arrList).map(x => x * x)
    
    Array.from(...[1,2,3],x => x * x);
    //  ----> [1,4,9]

2. Array.of()

  1. For a set of values, into an array

  2. If there are no parameters, an empty array is returned

    Array.of();      // []
    Array.of(1); // [1]
    Array.of(1,2,3); //[1,2,3]

3. find () and findIndex ()

  1. The method used to find the array to identify an eligible element parameter is a callback function

    [1,2,3,4,5].find(n => n > 1);        // 2
  2. findIndex method returns the first qualifying position of a member of an array parameter is a callback function

    [1,2,3,4,5].findIndex(n => n > 0)    //返回0,即第一一个符合条件元素的下标

4. fill()

  1. With the specified value, filling the array

  2. fill method may also have the second and third parameter is used to specify start and end positions

    [1,2,3].fill(7);     //[7,7,7]
    [1,2,3,4,5].fill(8,0,3)  //[8,8,8,4,5]

5. entries(),keys(),values()

  1. entries to traverse the key-value pairs

  2. keys used to iterate over the keys

  3. values ​​used to iterate values

  4. Available for ... of to loop through

    for(let index of ['a','b'].keys()){
        console.log(index);
    }
    
    for(let ele of ['a','b'].values()){
        console.log(ele);
    }
    
    for(let [index,ele] of ['a','b'].entries()){
        console.log(index,ele);
    }

6. includes the method

  1. includes an array of methods for an array contains the given value and returns a Boolean value

  2. The second parameter indicates the start of the search process of

    [1,2,3].includes(3);     //true
    [1,2,3].includes(1,1);       //false

7. flat(),flatMap()

  1. Array flat leveling function for nested into one-dimensional array

    [1,2,[3,4,5]].flat();        // [1,2,3,4,5]
  2. flat layer of embedded arrays default only leveled, flattened layers can develop mass participation by the way, the parameter defaults to 1

  3. No matter how many layers of nested, can be passed as a parameter by Infinity keywords, unified into one-dimensional arrays

8. copyWithin()

  1. The method copyWithin array instance, for the current internal array, copying the member of the specified location to another location, and then returns the current array

  2. It accepts three parameters

    • target: the array from that position to begin replacing
    • start: Alternatively, from the position to start reading the array
    • end: Alternatively, the position to stop reading the array
    [1,2,3,4,5].copyWithin(0,3);     //[4,5,3,4,5]

9. array vacancy

  1. Array array, i.e. gap only be separated by commas, and there is no set position value
  2. Since each of the different methods of processing space, so we should try to avoid vacancy

to sum up

  1. ES6, add extended operator array, extended operator is analogous to rest parameter array into effect is a comma-separated list of parameters
  2. ES6, add some methods arrays object instance

Guess you like

Origin www.cnblogs.com/mapengfei247/p/11116727.html