Are you familiar with operations on Javascript arrays? Here, I would like to share with you a brief introduction, including creating, adding, deleting, reading, and common methods and attributes of JavaScript arrays, I believe this article will surely help you gain some benefits.
Javascript array and operations
This article introduces the following aspects:
1. How to Create a JavaScript Array
2. How to add, delete, and read JavaScript arrays)
3. Common JavaScript Array methods and attributes
◆ How to create an array is generally divided into three types based on Initialization settings:
1. Create a JavaScript array:
Vararr = newArray ();
Key Point: use the new keyword to create an Array object Array (). The Array () object is a local class. You can use new to create an object and then use
2. Specify the array size when creating a JavaScript array:
Vararr = newArray (10); // an array with an initial size of 10 is created.
Note: When the array size is used for initialization, the array will be automatically extended without errors like in C. dynamic Growth is a property of js arrays. in addition, js supports a maximum length of 4294967295
3. Direct initialization:
Vararr = newArray ("crawler", "love", "Mao"); // The array or vararr = ["crawler", "love ", "Mao"]; // brackets can also declare an array object. Of course, similar to the C language, you can also define a 2-dimensional, 3-dimensional, and multi-dimensional array, which is not discussed here.
JavaScript array attribute: length
Arr. length returns the length of the array arr, which is common in traversing arrays in a loop. For example:
- For (vari = 0; I <arr. length; I ++ ){
- Execution part
- }
Access to array elements: arr [index], where index indicates the base number of the array. It starts from 0 and has a total of arr. length element. for example, if arr [0] accesses the first array element, arr [1] accesses the second array element .... operation Methods of arrays by analogy: first, we will give an overview of the common methods of these operation arrays (13)
- toString(),valueOf(),toLocalString(),join(),
- split(),slice(),concat(),
- pop(),push(),shift(),unshift(),sort(),splice()