JavaScript Array Object

Source: Internet
Author: User
Tags javascript array

Original: JavaScript Array object

One, create an array object

The array of JavaScript supports support for any data type, and does not specify that the array type can only store this type, as java,c#.

We can use the following three methods to create JavaScript array objects,

1,var arr=new Array ();

2, var arr=new array ([size]);//size is an integer that defines a fixed-length array

3,var arr=new Array (element1[,element2[,... elementn]);//elementn an element of an array

Two, array assignment

From the method of creating an array object above, only the array object created with the first and second methods needs to be assigned again, and the third method has been assigned at the time of creation. For the first and second methods to create an array object we can use the following methods to assign values

var arr1=new Array (); Arr1[0]=1;arr1[1]=2;arr1[2]=3;var arr2=New Array (3); arr2[0]=1;arr2[1]=2; arr2[2]=3;

However, when you create an array, you specify the length of the array object, you can only assign a value within the length range, and you cannot assign values beyond the range, for example, arr[3]=4 in the example above, it is not possible to assign a value successfully.

Of course, in addition to the above assignment, we can use the method provided by the array object to assign a value to the array, which is described in the following arrays method.

Three, the access of the array

JavaScript arrays are accessed in the same way as other languages, and are accessed by subscript arrays, as follows

var arr1=NewArray(); arr1[0]=1;arr1[1]=2;arr1[2]=3; document. Write (arr1[0]);
Four, modification of array elements

The modification of the JavaScript array elements is simple, as long as you add a new value like the subscript, as follows

var arr1=New Array(); arr1[0]=1; arr1[1]=2; arr1[2]=3; arr1[0]=4; document. Write (arr1[0]);

The output will be 4, not 1, so you can modify the success

Five, the third creation of array objects special description

If I want to use a third method to assign an integer to an array, for example, I want to assign a value of 1 in a set, you can use the

var arr=new Array (1);

Arr[0]=1;

or use

var arr=new Array ();

Arr[0]=1;

Instead of using var arr=new array (1) alone, this method only creates an array object with an array length of 1 and cannot be assigned a value, but the string is a var arr=new array ("a") that can be assigned a successful value;

Six, commonly used methods of array objects

1,concat method

The method returns a new array, which consists of the array and other elements, and does not require that the merged object be an array, which can be any element.

The following examples are given:

CodefunctionArrayconcat () {varARR1 =New Array(1, 2, 3);varValue = "JavaScript";varARR2 =New Array("C #", "Java");varARR3 = Arr1.concat (value, ARR2);Document. Write (ARR3.length+ "</br>");Document. Write (ARR3.toString() + "</br>"); }

The result of the output is

6
1,2,3,javascript,c#,java

2,join method

The method returns a string instead of an array, and the elements in the array are separated by the specified delimiter. The method is Arr.join (string), where

The string is optional, and if you do not specify a delimiter, the array elements are separated by commas.

The following example

Code   function arrayjoin ()        {            varnewArray(1, 2, 3);            var value = "-";            var arr2 = Arr1.join ();            document. Write (arr2.  Length + "</br>");            document. Write (arr2.  ToString() + "</br>");        }


The output is:

3,sort (Fun) method

sorts the elements of an array, the fun sort function is optional, does not specify a sort function, the ASCII table is sorted in ascending order, if the fun function is specified, then the function must return –1 or 0 or 1,-1 to indicate that the first argument is smaller than the second argument, and the 0 parameter is equal, 1 means the first parameter is larger than the second argument

Code  functionArraySort () {vararr =New Array(1, 3, 2, 0, 1, 4, 2); Arr.sort ();Document. Write (arr.toString() + "</br>"); Arr.sort (DESC);Document. Writeln (arr);Document. Writeln ("<br>"); Arr.sort (ASC);Document. Writeln (arr); }functionDesc (x, y) {if(x > Y)return-1;if(x < y)return1;if(x = = y)return0; }functionASC (x, y) {if(x > Y)return1;if(x < y)return-1;if(x = = y)return0; }

4,reverse method

The method reverses the given array.

The above method is also used frequently, other methods of use can refer to JavaScript use the document or can go to W3school view

Seven, two-dimensional array

Using the method given above to create an array object, you will find that you can only create arrays, and in development you will often use two-dimensional arrays, such as Jqgrid, which uses two-dimensional arrays to present the data, so we'll use JavaScript to create a two-dimensional array. The idea is simple, since it's an array, we'll create an array and then insert an array into the elements in the first array. This allows you to form a two-dimensional array, which gives the code

Code  varnewArray();  for (var i = 1; i <=; i++) {    newArray();     for (var j = 0; J <=, J + +) {        Aa[i][j] = i + j;     }}

Eight, the properties of the array

1,length often used to get the length of an array

2,propertype returns a reference to the object's prototype, using this property we can set our own method for the array object, such as I want the array object to have my own column maximum value method, you can use

Code  function Array_max ()
{
   var  This [0];

for (i = 1; i < this.) length; i++)
{
if (Max < this [i]) max = this [i];
}
return Max;
}
Array. prototype. max = Array_max;
var arr = new Array(1, 2, 3, 4, 5, 6);
var value = X.max ();

The log is written here, just start blogging, if there is something wrong in the place please specify or you have a better opinion, we can exchange, thank you.

JavaScript Array Object

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.