This is the 20th day of my participation in the August Text Challenge.More challenges in August

This part is not used very often, but make a note of it;

Create an array

When creating arrays using the Array constructor, you can omit the new operator and get the same result. You can also use Array literals. As with objects, the Array constructor is not called when creating arrays using Array literals. The Array constructor adds two static methods for creating arrays in ES6: from() and of(); From is used to convert a class array structure into an array instance;

Array.from()

The first argument to array.from () is an array-like object, that is, any iterable structure, or one that has a length attribute and indexable elements. This can be used in many situations;

  • The string is split into a single character array

    console.log(Array.from('Mannqo'));  // ["M","a","n","n","q","o"]
    Copy the code
  • Converts the collection and mapping to a new array

    const m = new Map().set(1.2).set(3.4); 
    const s = new Set().add(1).add(2).add(3).add(4);
    console.log(Array.from(m));   // [1, 2], [3, 4]]
    console.log(Array.from(s));   // [1, 2, 3, 4]
    Copy the code

    Print m mapping and S set as follows:

  • Shallow copy of an existing array

    const a1 = [1.2.3.4];
    const a2 = Array.from(a1);
    console.log(a1 === a2);  // false
    Copy the code
  • You can use any iterable

    const iter = {
        *[Symbol.iterator]() {
            yield 1; yield 2; yield 3; yield 4; }}console.log(Array.from(iter));  / / [1, 2, 3, 4]
    Copy the code
  • Arguments objects can be converted to arrays

  • Transform a custom object with the necessary attributes

Array.from() accepts an optional second mapping function argument that directly enhances the Array’s value; You can also accept a third optional argument that specifies the value of this in the mapping function, when the overridden this does not apply in the arrow function.

const a1 = [1.2.3.4];
const a2 = Array.from(a1, x= > x**2)
const a3 = Array.from(a1, function (x) { return x ** this.x }, { x: 2 })
[1, 4, 9, 16]
Copy the code

Array.of()

Array of () to convert a set of parameters to the Array, used to replace Arrray. Prototype. Slice. The call (the arguments);

console.log(1.2.3.4);  // [1, 2, 3, 4]
Copy the code

An array of space

When you initialize an array with an array literal, you use a string of commas to create a vacancy. ECMAScript treats the value of the corresponding index position between commas as empty; The ES6 new methods generally treat these empty Spaces as existing elements with a value of undefined;

const arr = [, , , , ,];
console.log(arr.length);  / / 5
Copy the code

In practice, you want to close the mic using array empty, if you want to use it, use undefined instead;

Array index

Fetching an element from an array by index number, adding an element to the end of an array by length attribute; An array can contain up to 4, 294, 967, 295 elements; Exceeding this range causes an error to be thrown; Creating an array with this maximum value as an initial value may cause the script to take too long.

Detection array

instanceof&Array.isArray

To determine whether an object is an array, use the instanceof operator if there is only one web page (a global scope).

console.log(arr instanceof Array);  // arr is a custom array
Copy the code

But when a page has multiple frames, there can be multiple versions of the Array constructor; You can use the array.isarray () method at this point;

Iterator method

keys()&values()&entries()

In ES6, the Array prototype exposed three methods for retrieving Array contents: keys(), values(), and entries(); Keys () returns an iterator to the array index; Values () returns an iterator for array elements and entries() returns an iterator for index/value pairs;

const a = ["a"."b"."c"."d"];
const aKeys = Array.from(a.keys());
const aValues = Array.from(a.values());
const aEntries = Array.from(a.entries());
Copy the code

Print the result as follows:

Copy and fill methods

Two new methods have been added to ES6: the bulk copy method copyWithin() and the fill array method fill(); Both methods need to specify a range of array instances that contain the starting index and no elements that end the index. Using this method does not change the size of the array;

fill()

The first parameter is the value of the fill, the second parameter index1 and the third parameter index2 are optional and represent the fill range (index1< INDEx2). The fill index is greater than index1 and less than index2. The filling number n is index2-index1;

const arr = [0.0.0.0.0];
arr.fill(5);   // [5, 5, 5, 5, 5]
arr.fill(6.3);   // [0, 0, 0, 5, 5]
arr.fill(6.1.3);   // [0, 5, 5, 5, 5]
Copy the code

If the index is too low, too high, and the direction (index1>index) is ignored; If the index part is useful, populate the available part;

copyWithin()

Unlike fill(), copyWithin() copies portions of the array in the specified range and inserts them at the beginning of the specified index; Start and end indexes are evaluated in the same way as fill();

  • The first parameter insert is the index of the insertion position, and the second parameter index1 is the index of the start position (default: 0). The third parameter index2 is the index value of the end replication. The number of replication n is index2-index1.

  • With only the first parameter, the default start copy location is [0,insert);

  • For an array arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Perform the following operations respectively

    arr.copyWithin(5);  // [0, 1, 2, 3, 4, 0, 1, 2, 3, 4] 
    arr.copyWithin(0.5);  // [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]
    arr.copyWithin(4.0.3);  // [0, 1, 2, 3, 0, 1, 2, 7, 8, 9]
    Copy the code

As with fill(), indexes too low, too high, and direction (index1>index) are ignored; If the index part is useful, populate the available part;