Operations on JavaScript array objects

Source: Internet
Author: User
Note: The JAVAScript Array and VBScript array are not only different in syntax, but also have many differences in use. 1. Create an array object. We will introduce how to create an array object in JAVAScript. (In fact, this method is essentially the same as the previous method, but it is different in statement writing. The above method is used in the program.

Note: The JAVAScript Array and VBScript array are not only different in syntax, but also have many differences in use.
 
1. Create an array object
 
     We will introduce how to create an array object of JAVAScript. (In fact, this method is essentially the same as the previous method, but it is different in statement writing. The above method is concise when the program is very short, in general, we recommend that you use the following method to create an array object .) The syntax for creating an array object is as follows:

1. When declaring an array, only several components in the array are declared.

Var Array object name = new Array (number of components );

Fruit = new Array (3 );  // Declare an array named fruit with three components. This is equivalent to declaring three variables at a time.

Then, several lines of program code must be prepared separately, and variable values must be entered in the collation.

Fruit [0] = "watermelon ";
Fruit [1] = "apple ";
Fruit [2] = "banana ";

2. All array components are directly given during Declaration. They are separated by commas and enclosed in parentheses. The number of components is the length of the array.

Var Array object name = new Array (component 1..., component N );

Var fruit = new Array ("watermelon", "apple", "banana ");

Note: In general languages, components in a number group must be of the same type, but different types of data can be put into arrays in JAVAScript.
 
 
2. attributes of an array object
 
JAVAScript provides the following attributes for array objects:

Format:
Array object name. Attribute
 
 
Description
1 constructor specifies the prototype Function
2. index indicates the index value of the array component.
3. input indicates the string in the Rule expression.
4 length: Obtain the array length (number of array components ).
5 prototype is used to create custom object attributes.
 
 
3. array object Method
 
JAVAScript provides the following methods for array objects:

Format:
Array object name. Method (parameter)
 
 
Description
1 concat (array 1, array 2,..., array N) combines multiple arrays into a new array
2 join (separator) combines an array into a string, separated by a specific character
3 pop () deletes the last component in the array and returns the component content.
4 push (component 1, component 2,..., component N) add one or more components to the end of the array and return the content of the last component.
5 reverse () reverse the index order of all components in the array (Transpose)
The first component changes to the last one, and the last component gets the first one.
6 shift () deletes the first component in the array and returns the component content.
7. slice (STARTS indexing and ends indexing) transfers the array content to a new PostgreSQL
8 sort () sorts the array content
9 splice () add or delete array Components
10 toSource () returns an array constant representing a specific array and can be used to create a new array.
11 toString () represents the array and its components in a string
12 unshift (component 1, component 2,..., component N) add one or more components to the beginning of the array and return the length of the last array.
13 valueOf () Get the array value
 
 
Note: Some methods, such as push, shift, unshift ...... Some versions of Internet Explorer do not support this feature. Pay special attention to this feature.
  
Example:


 
 
 
3. Two-dimensional array
 
          The array object of JAVAScript is actually a one-dimensional structure, but we can further design and use the one-dimensional array to put the array into the array, so that the components in the array are also arrays, it constitutes a two-dimensional array of JAVAScript. However, the use of two-dimensional arrays is prone to errors, so we will only introduce its concept here, and it is not recommended.


 

Iv. Delete elements in JS

 





 
Script
Array. prototype. del = function (k ){
Var len = this. length;
// Alert (len );
For (var I = k; I This [I] = this [I + 1];
}
This. pop ();
Return this;
}

Var array = [1, 2, 3, 4, 5, 6, 7];
Var a = array. del (2 );
Alert (a. join (","));
Var B = array. del (0 );
Alert (B. join (","));
Script


 

 4.1 // JS deletion element explanation

 

// Delete element in JS
Array. prototype. del = function (n)
{
 // N indicates the number of items, starting from 0.
 // Prototype is the object prototype. Note that the custom method is added for the object.
  If (n <0)// If n is less than 0, no operation is performed.
  Return this;
  Else
 Returnthis. slice (0, n). concat (this. slice (n + 1, this. length ));
 
}

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.