The Array.from of ES6 array method

Source: Internet
Author: User

The first thing to say is the array: the array is classified as object, which is a special object, and the index value of the array is similar to the object's key value.

Several points of note for arrays :

1. The length of the array is a readable property and cannot be changed, and the length of the array is based on the index maximum value.

2. The index value of an array can be a string, etc., but this does not affect the length of the array.

3. The array is automatically filled with the corresponding other index values based on the subscript maximum value, and the value is empty.

ES6 added a series of methods for arrays, such as array.of,array.from,array.find, etc.

The biggest difference between an array and an array of classes is that the array itself has an iterator iterator

Array.from The Array-like method is used to convert two types of objects to a true array: array-like objects (object) and Ergodic (iterable) objects (including ES6 new data structure set and MAP)

Any number with the Length property can be converted to an array.

Document.queryselectorall ()//Returns an object but has an iterator inside it, which loops
Document.getelementsbyclassname ()//class array collection, non-cyclic
function fn () {    = arguments// variable parameter set, non-cyclic }

Workaround

var arr1 = [].slice.call (arraylike); [' A ', ' B ', ' C ']//es5

Or

arr = Array.from (Likearr)//es6  does not change the original class array, returns the new array

You can also use an extension operator to resolve an indeterminate parameter to an array

function foo () {  var args = [... arguments];}// The iterator Interface (Symbol.iterator) is invoked behind the extension operator, and cannot be converted if an object is not deployed on this interface

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.

// [1, 4, 9]

With this feature, you can do this:

// [1, 0, 2, 0, 3]

Or so

function typesof () {  returntypeof Value)}

Array.from () can convert various values to real arrays, and also provides map functionality. This actually means that as long as there is an original data structure, you can manipulate its value first, then turn it into a canonical array structure, and then you can use a large number of array methods

// [' Jack ', ' Jack ']

For browsers that do not support array.from, you can use the Array.prototype.slice method to compensate


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

  

  

The Array.from of ES6 array method

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.