JS array management [Add, delete, change, check]

Source: Internet
Author: User
Tags javascript array

Today, when designing a form, we encountered some problems dealing with arrays, such as how to create an array, and then the method of adding and deleting the array. See API

Ff:firefox, N:netscape, Ie:internet Explorer

Method Description FF N IE
Concat () Joins two or more arrays and returns the result. 1 4 4
Join () Put all the elements of the array into a string. element is delimited by the specified delimiter. 1 3 4
Pop () Delete and return the last element of the array 1 4 5.5
Push () Adds one or more elements to the end of the array and returns the new length. 1 4 5.5
Reverse () Reverses the order of the elements in the array. 1 3 4
Shift () Delete and return the first element of the array 1 4 5.5
Slice () Returns the selected element from an existing array 1 4 4
Sort () Sorting elements of an array 1 3 4
Splice () Deletes the element and adds a new element to the array. 1 4 5.5
Tosource () The source code that represents the object 1 4 -
ToString () Converts the array to a string and returns the result. 1 3 4
toLocaleString () Converts the array to a local array and returns the result. 1 3 4
Unshift () Adds one or more elements to the beginning of the array and returns the new length. 1 4 6
ValueOf () Returns the original value of an array object 1 2 4

Note: JavaScript arrays and VBScript arrays are not only syntactically different, but also have a number of differences when used, so be aware of the distinction.

First, set up an array object

Let's introduce the method of setting up the JavaScript array object. (In fact, this method and the previous method is essentially the same, only in the wording of the statement is different, the above method in the program is very short use of relatively concise, in general I would recommend that you use the following methods to build an array object. There are two types of syntax for building an array object:

1. When declaring an array, only a few components are declared within the array.

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

Fruit = new Array (3); Declares an array named fruit, with three components, which is equivalent to declaring three variables at a time

Then a few lines of program code must be prepared separately, preface the variable values.

Fruit[0] = "Watermelon";
FRUIT[1] = "Apple";
FRUIT[2] = "banana";

2, the declaration is directly given all the array components, separated from each other by commas, enclosed in parentheses, the number of components is the array length.

var Array object name = new Array (component one ..., component n);

var fruit = new Array ("Watermelon", "apple", "banana");

Note: Components within an array in a generic language must be of the same type, but in JavaScript you can put different types of data into the array.

Second, the properties of the array object

JavaScript provides several properties for an array object:

Use Format:
Array object name. Property


Order Property name use description  
1 constructor Specifies to establish the ground image prototype (prototype) function  
2 Index Represents the index value of an array component  
3 input represents a string in a regular expression.  
4 Gets the array length (number of array components).  
5 prototype used to build custom object properties  

Three, method   for array objects;

JavaScript provides several methods for array objects:

Use Format:
Array object name. Method (parameter)

Ordinal method name usage description  
1 concat (array 1, array 2,..., array N) combines multiple arrays into a new array  
2 Join (delimited character) combines an array into a string, separating   with a specific character;
3 pops () deletes the last component in the array and returns the contents of the component  
4 push (component 1, component 2,..., component N) Complements one or more components to the end of the array and returns the last component content  
5 reverse () reverses the index order of all components in the array (transpose)
The first component becomes the last, and the last component gets to the front  
6 shift () deletes the first component within the array and returns the contents of the component  
7 Slice (start index, end index) transfers the contents of the array to a new Fir  
8 sort () sorts the contents of the array  
9 Splice () Adds or removes an array component  
Tosource () returns an array constant representing a specific array, which can be used to create a new array  
One toString () to represent the array and its components   in a string;
The Unshift (component 1, component 2,..., component N) complements one or more components to the front of the array and returns the last array length  
ValueOf () gets the array value  

Note: Some of these methods, such as: Push, Shift, Unshift ... Some versions of IE are not yet supported and should be used with special care.  
   
Example:

<script language = "JAVAScript";
<!--
     var fruit = new Array ("Watermelon", "banana", "apple");   //Declare an array and assign values to three kinds of fruit in the array
   // The first time the array "fruit" is displayed, the contents of the 3 variables,<br> are newline characters
    document.write (fruit[0] + "<br>" + fruit[1] + "< Br> "+ fruit[2] +" <br> "+ fruit);
    document.write ("    // The second display array,     with (document)     {
    Write (Fruit.reverse () + "     write (Fruit.join (",") + "< Hr> ");   //combine the array into a string with", "Separate
    write (Fruit.sort () +"     write (fruit.length +    
-->
</script>

JS array management [Add, delete, change, check]

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.