Summary of array usage in JavaScript

Source: Internet
Author: User
Tags array length data structures
first, the concept of the array 1.1 What is an array

An array is an ordered list of data. Each value in an array is called an element of an array. Each element in the array has a position, which is called an index (subscript, index). The index of an array is the same as the one that starts with 0, and the type of the element does not have any restrictions. In other words, the same array can be method number, String, Boolean, object, and so on. Can be put into any type at the same time. Even an element in an array can be another array (a multidimensional array). 1.2 The features of arrays in JavaScript

Although each language has an array of these data structures, the JavaScript arrays are quite different from each other. The length of the array can be changed dynamically. Different data types can be stored in the same array. Ordered collection of data each array has a length property that represents the number of elements in the array, and the creation of the array

There are two basic ways to create an array: The literal way and the 2.1 literal form of the constructor

Array literal: All elements are enclosed in square brackets, with commas separated by different elements.

For example: ["A", 5, "B"]

Creates an array of length 3 and initializes 3 elements: "abc" "B" "D"
var colors = ["abc", "B", "D"];
Creates an array of empty arrays of length 0. There is no value in
var colors = [];
Do not add commas to the end of the last element, add commas although the syntax is not problematic, but in different browsers may get different results
var colors = [5, 6, 7,];//So the length of the array may be 3 or 4. You should avoid this way of creating different results in different browsers.
   
   
    
    1
    
    2
    
    3
    
    4
    
    5
    
    6 7
   
   
2.2 How constructors are structured

The constructor is used when the object is created. The array's constructor array ()

For example: new Array (array length);

Create an empty array of length 0
var colors = new Array ();
Create an array of length 5. The default value for the elements of each array is undefined.
var colors = new Array (5);
Create an array of length 3, and 3 elements are "blue" "Red" "green"
var colors = new Array ("Blue", "Red", "green");
   
   
    
    1
    
    2
    
    3
    
    4
    
    5
    
    6
   
   

Note: When you use a constructor to create an array object, do not add parentheses after the last element, or you will get an error. This is wrong: the new Array ("a") uses the constructor if only one number value is passed in, the value must be >= 0, otherwise an error will be made. When you use a constructor to create an array object, the New keyword can be omitted. For example: Array (5) This is OK. Iii. Accessing and modifying elements in an array

Accesses an element in an array using the index.

If the length of the array is 5, then the index of the array is 0,1,2,3,4

Create a data length of 5
var arr = [5, 7];
Alert (arr[0]);  Gets the element with subscript 0,  that is: "
alert (arr[2]);  " Get the element with subscript 2,  that is: a

//
arr[1] = m;  Assign an element with a subscript of 1 to a value of 100.
   
   
    
    1
    
    2
    
    3
    
    4
    
    5
    
    6 7
   
   
Four, the length of the array 4.1 Get the length of the array

Each array has a property called length that represents the length of the array (that is, the number of elements).

var arr = [5, 7];
alert (arr.length);  Eject: 5
   
   
    
    1
    
    2
   
   
4.2 Modifying the length of an array

In the general strongly typed language, the length of the array is fixed, that is, once the array is created successfully, the length of the array cannot be changed.

However, JavaScript is a weak type of dynamic language, and the length of the array can be dynamically changed as needed during program execution

= = The Length property of the array is not read-only but can be modified. = = Modify the length of the array directly to the specified number by setting the value of length.

var arr = ["A", 8, "BC"]; The length of the array is 3 
arr.length = 6;//Modify the length of the array to 6 
alert (arr.length);  The length of the array has been modified to 6, so output 6 here.
The values for the elements labeled 3, 4, and 5 are undefined.
alert (arr[3]);  Pop-up: undefined.

arr.length = 2;//Modify the length of the array to 2, then the element subscript >= is automatically removed from the array.
   
   
    
    1
    
    2
    
    3
    
    4
    
    5
    
    6 7
   
   
Dynamically modify the length of an element by assigning a value to the last element.
var arr = [4, 6, 8];
Assign a value of 10 to the element that is subscript.  Since the initial length is 3, the length of the array will automatically grow to one arr[10 when the assignment is completed
;
alert (arr.length);  Pop-up: one
//No assigned element defaults to undefined
alert (arr[5]);//popup: Undefined

alert (arr[20);//eject: undefined
alert (arr.length); The length is still one by one.  The previous line of code simply accesses the element without an assignment operation, and does not cause the array length to change
   
   
    
    1
    
    
Related Article

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.