Simple Array application

Source: Internet
Author: User

I. Definition of Arrays
There are three methods to define arrays in javascript:
1. The number of arrays is defined as: var arr = [1, 2, 3];
2. Use constructors to define them, for example, VAR arr1 = new array ();
3. Use other variables to define an array
In the defined array, VAR arr = [1, 2, 3] is equivalent to VaR arr = new array (1, 2, 3),

VaR arr = [3] and VAR arr = new array (3) are not equivalent. The latter refers to the length.

Element
Ii. Array application
1. Split the string into an array using the split () method.
<Script language = "JavaScript">
VaR STR = "Monday, Tuesday, Wednesday, Thursday, Saturday, Sunday ";
VaR arr = Str. Split (",");
Document. Write (ARR. tostring ());
</SCRIPT>
2. Use of array elements. arrays use array subscript to index array elements (in

In JavaScript, a comma is equivalent to a plus sign)
3. Add array elements
VaR myarr = new array (100, true, "string"); // there are only three elements, so the most

The size is marked as 2
Arrelement (myarr );
Myarr [3] = true; // Add an element
Arrelement (myarr );
4. Multi-dimensional arrays cannot be defined in Javascript. You can use this method to define multi-dimensional arrays.
<Script language = "JavaScript">
VaR myarr1 = new array (1, 2, 3 );
VaR myarr2 = new array ("string 1", "string 2", "string 3 ");
VaR myarr3 = new array (true, false, true );
VaR myarr = new array (myarr1, myarr2, myarr3 );
Document. Write (myarr [0] [0], "<br>"); // result 1: Understanding is equivalent to myarr [0]

The first array is myarr1, and myarr [0] [0] is equivalent to myarr1 [0], which is the first of myarr1.

Elements
Document. Write (myarr [1] [2], "<br>"); // The result is string 3.
Document. Write (myarr [2] [1], "<br>"); // The result is: false.
</SCRIPT>
5. Convert the array into a string
VaR myarr = new array (100, true, "string ");
Document. Write (myarr. tolocalestring ());
Document. Write (myarr. Join ("-"); // join cannot be converted to a local string.
6. add elements to the array, delete the elements of the array, and replace the elements of the array.
Array. unshift (1); // It is added to the front of the array element, that is, the header of the array.
Array. Push (1); // On the contrary, the array element is added to the end of the array.
Array. splice (1, 0, "this"); // Add an array element.

Any location <note that the second parameter is 0>
Array. Concat (2); // Add an array element or add it to the end of the array element

New array, will not affect the array itself
Delete elements in the array header: array. Shift ();
Delete the specified element: the position of the array. splice () // element, the specified length
Replace the specified element: array. splice (, "this"); // The Position of the element is the subscript, element

Is a string, the replacement string
 

 

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.