Es Getting Started--expansion of arrays

Source: Internet
Author: User

1, Array.from ()

This method is used to convert two types of objects into real arrays: arrays-like objects (Array-like object) and Ergodic (iterable) objects, including ES6 new data structure set and map. Here is an array-like object, Array.from it into a true array.

Let Arraylike = {    "0": "A",    "1": "B",    "2": "C",    Length:3}; // the writing of ES5 var arr1 =//["A", "B", "C"]//ES6 The wording let arr2 =   //["A", "B", "C"]

2, Array.of ()

This method is used to convert a set of values into an array. The main purpose of this method is to compensate for the insufficiency of the array constructor function array (). Because the number of parameters is different, the behavior of array () can vary.

// [3,11,8] // [3] // 1      //[]//[]//[3,11,8]             

Array.of can basically be used to replace array () or new array (), and there are no overloads due to different parameters, and it behaves very uniformly.

3. Copywithin () of the array instance

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

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

The Find method of an array instance, which is used to find the first qualifying array member, whose argument is a callback function, where all array members execute the callback function sequentially until they find the first member with a return value of true, and then return the member and return undefined if there are no eligible members.

The FindIndex method of an array instance is very similar to the Find method, returning the position of the first qualifying array member, with the only difference being that if all members are not eligible, then 1 is returned, not undefined.

5. Fill of Array instance ()

The Fill method fills an array with the given value. The initialization of an empty array is very convenient, and the elements already in the array are erased. The Fill method can also accept the second and third parameters, which specify the starting and ending positions of the fill.

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

ES6 provides three new methods, entries (), keys (), and values () to iterate through the array. They all return a Ergodic object that can be traversed with a for...of loop, the only difference being that keys () is the traversal of the key name, values is the traversal of the key value, and entries () is the traversal of the key-value pair

 for (Let index of [' A ', ' B '].keys ()) "    {//0 1}            for" Let elem of [' A ', ' B ' ].values ()) {    //' a ' B ' (but tested, error in Chrome browser)}            for ( Let [Inde,ele] of [' A ', ' B '].entries ()) {    //0 "a" 1 "B"}

7. Includes () of the array instance

The method returns a Boolean value that indicates whether an array contains the given value, similar to the includes method of the string, which belongs to ES7, but is already supported by the Babel transcoding device. Before the method, we usually use the array of IndexOf method, check whether to include a value, the IndexOf method has two drawbacks, one is not enough semantics, its meaning is to find the first occurrence of the parameter value position, so to compare whether it is not equal to 1, the expression is not intuitive. The second is that it is internally judged using a strictly equivalent operator (= = =), which results in a miscarriage of Nan.

Es Getting Started--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.