class arrays and their transformations

Source: Internet
Author: User

What is an array of classes

? Broadly speaking, an object with a length property is an array of classes , but the class array, as its name implies, is just like an array but not an array, so it often does not have an array of methods, such as, forEach() push() , shift() etc.

? In the JavaScript world there are many arrays of classes, such as,,, and NodeList HTMLCollection NameNodeMap DOMTokenList functions arguments ... They're all objects with length.

Class Array Conversion Group

? It says that an array of classes is just a way to look like an array but not a lot of arrays, so it's not easy to operate so we can convert them to arrays

// ES3,5Array.prototype.slice.call//or applyArray.apply(null,// apply的第二个参数接受一个数组或者类数组// ES6Array.from(arrlike)[...arrlike// 前提是这个arrlike得具备迭代器接口[Symbol.iterator]。而以上三种有length即可

? It was previously said that an array of classes is an object with the length property, which is not exactly defined. Because there's something in there that needs to be noticed, the array is labeled as a number , but as the previous definition of the class array has only the length property, it does not look like an array, and its properties may not be strings in numeric form. Of course, arrays can also define non-numeric types of strings as properties, after all, the array itself is an object, but it is mainly in the form of a numerical subscript to store data, and those attributes are not counted into length

? so when an array of classes is converted to arrays, the corresponding data is not stored in the created array if the attribute in the object is not a string in numeric form, or if the value is greater than or equal to length.

Vacancies are filled with undefined instead of empty units

ConstArrlike1= { 0: ' A ', 1: ' B ', ' 2 ': ' C ', length: 3 }Console.Log(Array. from(ARRLIKE1))//[' A ', ' B ', ' C ']ConstArrlike2= {  0: ' A ', ' A ': ' B ', ' 2 ': ' C ', length: 3 }Console.Log(Array. from(Arrlike2))//[' A ', Undefined, ' C ']ConstArrlike3= {  1: ' A ', length: 1 }Console.Log(Array. from(ARRLIKE3))//[undefined]

class arrays and their transformations

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.