Extension of the Es6-array object

Source: Internet
Author: User

1.array.from ()
(1) converting to an arrayThe ①array.from () method canclass Array Object (The so-called array-like object, the intrinsic feature is only one point, that is, the length property must be. Therefore, any object with the Length property can be converted to an array by means of the Array.from method , at which point the extension operator cannot be converted. )and Ergodic (iterable) objects into real arrays. Includes ES6 new data structure set and map.
② Common class array objects, such as nodelist collections obtained by DOM operations, or arguments objects inside a function, can be converted to arrays by the Array.from () method. (1) Convert nodelist
(2) Convert arguments object
③ as long as the data structure of the iterator interface is deployed, Array.from () can convert it to an array.
④ If the argument is an array, it returns an identical new array.
⑤ extension operator ... Some data structures can also be converted to arrays,the Iterator Interface (Symbol.iterator) is invoked behind the extension operator, and cannot be converted if an object is not deployed on this interface.
(2) The second parameter, Array.from (), can also accept the second parameter, which acts like an array's map method, which is used to process each element and put the processed value into the returned array.


(3) If the This keyword is used in the map function, you can also pass in the third parameter of Array.from () to bind this. (4) As long as there is an original data structure, it can be processed, converted to the canonical array structure, and then using the array method. In the following code, the first parameter of Array.from () specifies the number of times the second parameter runs.
(5) Another application of Array.from () is to convert the string to an array, returning the length of the string, which correctly handles various Unicode characters, and avoids a bug that is larger than the \UFFFF character counted as two characters.
2.array.of ()
The Array.of () method is used to convert a set of values to an array, in order to construct the insufficiency of the array of functions, because the number of arguments differs, which causes the array () to behave differently.
Array.of () can basically be used instead of an array () or new array (), with uniform behavior and no overloads due to different parameters.
Array.of () always returns an array of arguments, or an empty array if there are no arguments.
3. Copywithin () of an array instance
The Copywithin method of an array instance copies the members of the specified position to another location within the current array, overwrites the original member, and returns the current array. Accepts three parameters (1) Target: required, where to start the replacement data (2) Start: Optional, starting from this position to read the data, the default is 0, if negative, indicating the reciprocal. (3) End: Optional, stop reading data to this position, default is Array.Length, if negative value indicates reciprocal. These three parameters should all be numeric values, if not automatically converted to numeric values.
Parameter 0,2, starting from arr[0] to replace the data, replaced by the beginning of arr[2], up to the end of the data, a total of three, covering the original 123.
4. Find () and FindIndex () methods for array instances
(1) Arr.find (function (Value,index,arr) {...}) The Find method of an array instance is used to find the first qualifying array member, the parameter is a callback function, and all the cannibal members execute the function sequentially until they find a member with a return value of true and return the member, and return undefined if not found.
(2) The FindIndex method of an array instance is similar to the Find method, but it returns the position of the first qualifying array member, or 1 if it is not found.
Both methods can accept the second parameter to bind the This object of the callback function, and both methods can find Nan, which makes up for the lack of the IndexOf method.

5. The fill () method of an array instance
(1) Fill method fills an array with the given value
(2) The Fill () method can also accept the second and third parameters, indicating the starting and ending positions of the fill. (starting from starting position, replacing starting position, ending before end)
6. Entries () of the array instance, keys () and values ()
(1) ES6 provides three new methods, entries (), keys (), and values () to iterate through the array, all of which return a Walker object that can be traversed with for...of, the only difference being that keys () is the traversal of the key name, and values () is the traversal of the key value, Entries () is a traversal of key-value pairs.

(2) If you do not use the For...of loop, you can manually invoke the next method of the Walker object to traverse it.

7. Includes () method for array instances
(1) The includes () method of an array instance returns a Boolean value that indicates whether the array contains the given value, similar to the includes method of the string. ①
② the second parameter of the method indicates the starting position of the search, the default is 0, if the argument is negative, the position of the reciprocal, and if the countdown number is greater than the length of the array, it is reset to start at 0. The IndexOf () method is not semanticallyThe return value is the first occurrence of the found parameter, and it is internally judged with the strict equality operator, so Nan is not found.
The has method of the ③MAP structure is used to look up the key name, such as Map.prototype.has (key), the has method of the set structure is used to find the value, such as Set.prototype.has (value)
8. The empty array
(1) The empty array means that there is no value in the array position, such as an array returned by the array constructor is an empty space.
(2) The vacancy is not a undefined, the value of a position is undefined is still a value, the vacancy is not any value.
(3) ES5 processing of the empty space (), filter (), every (), and some () will skip the empty space. Map () Skips the empty space, but retains this value join () and ToString () treat the vacancy as undefined, while undefined and null are processed into an empty string.
ES6 will clearly convert the vacancy to undefined. ①array.from () shifts the array vacancy to the Undefined② extension operator and also shifts the vacancy to undefined③copywithin () will be copied with the empty space. ④fill () treats the vacancy as a normal array position. ⑤The For...of Loop also iterates through the slots. ⑥entries (), keys (), values (), find (), and FindIndex () will process the empty space into undefined.


Extension of the Es6-array object

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.