Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's so special about JavaScript's array?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you what is special about the array of JavaScript. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Array is the most commonly used data structure for front-end developers. We operate arrays all the time in the project, such as storing the data of list components in an array, and storing the data that needs to be rendered into bar graphs in an array. Although we often use arrays, many people do not understand the nature of JavaScript arrays.

The use of JavaScript array

Array is our most commonly used data structure, and many array-based operations are familiar enough. We will not list the API of the array here, because this part of the MDN array is authoritative and comprehensive enough. We will briefly introduce the key array methods to pave the way for the following content.

Creation and initialization of JavaScript array

If you have learned other languages like c++/java before, you may use the following method to create and initialize an array:

ConstappleMac=newArray ('MacBookAir','iMac','MacBookPro','Macpro')

Of course, this is possible in JavaScript, but it is not a mainstream approach. Usually people create and initialize arrays literally:

ConstappleMac= ['MacBookAir','iMac','MacBookPro','Macpro']

Two new methods have been introduced in es6, and you can also create arrays:

Array.of () returns an array of all parameters, regardless of the number or type of parameters, or an empty array if there are no parameters.

Array.from () creates a new array from an array of classes or iterable objects

These two methods solve two problems respectively, and Array.of () solves the strange behavior caused by a single number when the constructor method creates an array.

Consta=newArray (3); / / 3) [empty×3] Constructor method A single array is used for array length

Constb=Array.of (3); / / [3]

Array.from () solves the conversion problem of "class array". Before, we generally use the partial Hack method of Array.prototype.slice.call (arguments) to convert the class array to the array. The emergence of Array.from () standardizes it. In the future conversion, we'd better follow the standard Array.from () method for conversion.

The above is what's special about the JavaScript array that the editor shared for you. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report