ES 6: Expansion of arrays

Source: Internet
Author: User

1.array.from ()

The Array.from method is used to convert two types of objects into real arrays: arrays-like objects and Ergodic (iterable) objects.

Here is an array-like object, Array.from it into a true array.

As long as the data structure of the iterator interface is deployed, Array.from can convert it to an array.

In the above code, both the string and the set structure have iterator interfaces, so the Array.from can be converted to a true array.

  For browsers that have not yet deployed this method, you can use the Array.prototype.slice method instead:

Const ToArray = (() = Array.from? Array.from:obj = [].slice.call (obj)) ();

Array.from can also accept the second parameter, which acts like a map method of an array, which is used to process each element and put the processed value into the returned array.

Array.from (arraylike, x = x * x); // equivalent to Array.from (arraylike). map (x = = x * x);

2.array.of ()

The Array.of method is used to convert a set of values to an array.

The main purpose of this method is to compensate for the insufficiency of the array constructor function array (). Can basically be used to replace array () or new array (), and there is no overload caused by different parameters.

3.copyWithin ()

The Copywithin method of an array instance that copies the members of the specified position to another location within the current array (overwriting the original member), and then returns the current array.

this. Length)

It accepts 3 parameters.

target(required): Replace data from this location

Start (optional): Reads data from this location, which defaults to 0. If negative, indicates the reciprocal

end(optional): Stops reading data before it is reached, which is equal to the array length by default. If negative, indicates the reciprocal

All 3 parameters should be numeric and, if not, automatically converted to numeric values.

The code above indicates that the members (4 and 5) from position 3rd until the end of the array are copied to the position starting at position No. 0, with the result overwriting the original 1 and 2.

  For platforms that do not have a Copywithin method deployed with Typearray, the following wording is required:

4. Find () and FindIndex () for array instances

The Find () method of an array instance that is used to find the first qualifying array member. Its argument is a callback function, in which all array members execute the callback function sequentially until they find the first member to return a value of true, and then return the member. If there are no eligible members, the undefined is returned.

The above code finds the first member in the array that is less than 0.

In the above code, the callback function of the Find method can accept 3 parameters, in turn, the current value, the current position, and the original array.

The FindIndex method of an array instance is used very similarly to the Find method, returning the position of the first qualifying array member, or 1 if all members do not meet the criteria.

5. Fill () of the array instance

The Fill method populates the array with the given values.

The above code shows that the Fill method is convenient for initializing an empty array. The elements that are already in the array are erased. The Fill method also accepts the second and third parameters, which specify the starting and ending positions of the fill.

The above code indicates that the Fill method starts at position 1th, fills the original array with 7, and ends before the 2nd position.

6. Entries (), keys () and values () for the array instance

ES6 provides 3 new methods,--entries (), keys (), and values ()--for iterating over an array. They all return a Walker object that can be iterated through for...of, the only difference being that keys () is the traversal of the key name, and values () is the traversal of the key value, and entries () is the traversal of the key-value pair.

7. Includes () of an array instance

The Array.prototype.includes method returns a Boolean value that indicates whether a number group contains the given value, similar to the includes method of the string. This method belongs to ES7, but the Babel transcoding device has been supported.

The second parameter of the method represents the starting position of the search, which defaults to 0. If the second argument is a negative number, it indicates the position of the reciprocal, and if this position exceeds the array length, it will be reset starting at 0.

Without this method, it is common to use the IndexOf method to check for a value, but the IndexOf method has two drawbacks: one is not enough semantics, its meaning is to find the first occurrence of the parameter value, so to compare whether it is not equal to 1, the expression is not intuitive; = = =) Judgment can result in a miscarriage of Nan.

  For platforms that do not have this method deployed, you can use the following code:

8. The empty array

There is no value for a position of the array's empty exponent group. The ES5 ignores the empty space, and ES6 explicitly shifts the vacancy to undefined, which is not ignored.

-Array.from () will switch the vacancy to undefined

-Extension operator (...) will also convert the vacancy to undefined

-Copywithin () will copy the empty space together

-Fill () treats the vacancy as a normal position

-The For...of loop will also traverse the empty space

In the above code, the array arr has two slots, and For...of does not ignore them. If you change to a map method, you will skip the empty space.

-Entries (), keys (), values (), find (), and FindIndex () will process the empty space into undefined

Since the processing of vacancies is very non-uniform, it is advisable to avoid the presence of vacancies.

ES 6: Expansion of arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.