Delete array elements from the JS array! -Collection

Source: Internet
Author: User
Tags javascript array
An array is composed of many variables with the same name. The usage of an array is the same as that of a common variable. It can also be stored in any data type, the only difference is that it occupies a continuous space in the memory. You can number them sequentially and use them by number. An array can declare and use multiple variables at a time. The use of JavaScript arrays is different from that of VBScript. When using JavaScript arrays, enclose them with brackets []. Different variables are separated by commas.
VaR array name;
Array name = [1,..., n];

For example, I want to define an array "Fruit", which should contain three types of fruit "watermelon", "apple", and "banana". We need to write:

VaR fruit;
Fruit = ["watermelon", "apple", "banana"]; // assign values to the three fruits in the array

"Fruit [0]" is "watermelon", "fruit [1]" is "apple", and "fruit [2]" is "banana ", "Fruit" is "watermelon, apple, banana ". (JavaScript starts counting from "0". This principle must be remembered .)

If you want to assign a new value to the variable in the array, such as changing "apple" to "grass Mold", you need to write it like this:

Fruit = ["watermelon", "apple", "grass Mold"]; // assign a value to the variable in the array "Fruit"

Because the variables "fruit [0]" and "fruit [1]" in the array remain unchanged, you can also write as follows:

Fruit = [fruit [0], fruit [1], "grass Mold"]; // change the variable "fruit [2]" in the "Fruit" array to "grass Mold"

Note: even if the values of the variables "fruit [0]" and "fruit [1]" are not changed, you must enter them. Otherwise, the original value will be lost and changed to "undefined ".

In fact, the array variables do not need to be written out. If you write this array as follows:

VaR fruit;
Fruit = ["watermelon", "grass Mold"]; // assign values to the three fruits in the array

"Fruit [0]" is "watermelon", "fruit [1]" is "undefined", and "fruit [2]" is "Straw ", "Fruit" is "watermelon, grass Mold ".

If you write this array as follows:

VaR fruit;
Fruit = ["watermelon", "banana",]; // assign values to four fruits in the array

In this case, the four variables "fruit [0]" in the array are "watermelon", "fruit [1]" is "grass Mold", and "fruit [2]" is "undefined ", "fruit [3]" is "undefined", "Fruit" is "watermelon, grass mold ,,".

The following is an example:

<Script language = "JavaScript">
<! --
VaR fruit; // declare an array (variable)
Fruit = ["watermelon", "banana", "apple"]; // assign values to the three fruits in the array
// Display the content of the three variables in the array "Fruit" for the first time. <br> it is a line break.
Document. Write (fruit [0] + "<br>" + fruit [1] + "<br>" + fruit [2] + "<br>" + fruit );
Document. Write ("<HR>"); // insert a horizontal line as a separator
Fruit = [fruit [0], fruit [1], "grass Mold"]; // change the variable "fruit [2]" in the "Fruit" array to "grass Mold"
// Display the content of the three variables in the array "Fruit" for the second time. <br> it is a line break.
Document. Write (fruit [0] + "<br>" + fruit [1] + "<br>" + fruit [2] + "<br>" + fruit );
Document. Write ("<HR>"); // insert a horizontal line as a separator
Fruit = ["watermelon", "grass Mold"]; // assign a value to the variable in the array "Fruit"
// For the third time, the three variables of the array "Fruit" are displayed. <br> it is a line break.
Document. Write (fruit [0] + "<br>" + fruit [1] + "<br>" + fruit [2] + "<br>" + fruit );
Document. Write ("<HR>"); // insert a horizontal line as a separator
Fruit = ["watermelon", "banana",]; // assign a value to the variable in the array "Fruit", increasing the array to four variables.
// Display the four variable content of array "Fruit" for the fourth time. <br> it is a line break.
Document. write (fruit [0] + "<br>" + fruit [1] + "<br>" + fruit [2] + "<br>" + fruit [3] + "<br>" + fruit );
Document. Write ("<HR>"); // insert a horizontal line as a separator
-->
</SCRIPT>

 

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. There are three components, which is equivalent to three variables declared 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:

<Script language = "JavaScript">
<! --
VaR fruit = new array ("watermelon", "banana", "apple"); // declare the array and assign values to the three fruits in the array
// Display the content of the three variables in the array "Fruit" for the first time. <br> it is a line break.
Document. Write (fruit [0] + "<br>" + fruit [1] + "<br>" + fruit [2] + "<br>" + fruit );
Document. Write ("<HR>"); // insert a horizontal line as a separator
// Display the array for the second time. <HR> inserts a horizontal line as the separator line.
With (document ){
Write (fruit. Reverse () + "<HR>"); // reverse the Array
Write (fruit. Join (",") + "<HR>"); // combine the array into a string, separated ","
Write (fruit. Sort () + "<HR>"); // sort the array content
Write (fruit. Length + "<HR>"); // calculate the length of the array.
}
-->
</SCRIPT>
 
 

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.

/*************************************** *******************/
JS deletes array elements:

<Script language = "JavaScript" type = "text/JavaScript">
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 <0, no operation is performed.
Return this;
Else
Return this. Slice (0, n). Concat (this. Slice (n + 1, this. Length ));
/**//*
Concat method: returns a new array consisting of two or more arrays.
Here we return this. Slice (0, N)/This. Slice (n + 1, this. length)
In the middle of the new array, the nth entry is missing.
Slice Method: return a segment of an array with two parameters, respectively specifying the start and end positions.
*/
}
// Let's try this method we have added.
VaR test = new array (0, 1, 2, 3, 4, 5 );
Test = test. Del (3); // The value starts from 0. In this case, 4th items are deleted.
Alert (test );
</SCRIPT>

/*************************************** *********************/

1. An important feature of JS array that is different from other languages is that it can contain different data types in different rows. This is also a feature of weak type languages such as Js!

2. The JS array also has a major feature: it does not support multi-dimensional arrays. This is not as good as vbs And hehe, but it is not impossible. We can include arrays in arrays or use correlated arrays.
3. VaR Al = new array (1, 2, 3, 4, 5, 6, 7, 8, 9, "Day bombing", "javascript entry series", "vs2005 series controls demonstration ") // declare an array Al and assign the Initial Value
// The following two sentences insert two elements at the specified position of the array, so that the initial element will be extended.
Al [5] = "thcjp ";
Al [9] = 8867;
// Read
For (VAR I = 0; I <Al. length; I ++)
{// Declare a variable I with a value of 0 and loop to the items in the array
Document. write (I + 1 + "· <B>" + Al [I] + "</B> <br> "); // print all elements of the array on the screen. Pay attention to the Code in the parentheses.
}

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.