How to use JavaScript Arrays

Source: Internet
Author: User
Tags javascript array

Javascript is a non-type language, so an array element can have any data type. different elements of the same array can have different types, array Element settings can contain other arrays, so that you can create a complex array.

At this point, javascript, as a scripting language, is more flexible than the strict object-oriented c ++, c #, and java.

In javascript1.1 and later versions, arrays are created using the constructor Array () and the new operator. The following three methods can be used to create arrays in javascript.

var a = new Array();var b = new Array(5, 4, 3, "first", "test,string");var c = new Array(20);a[1.23] = "test";document.write("a[1.23]="+a[1.23]);

Program output: a [1.23] = test

I believe that every person who learns javascript from a strong programming language will be surprised by the above operations. Float data is also used as the subscript of the array. In fact, it is not as you think.

Javascript converts a negative number, floating point number, or boolean type, object, or other value to a string. Use the generated string as the property name of the object, instead of defining a new array element.

The preceding instance creates an attribute named "1.23" for.

var a = new Array();var b = new Array(5, 4, 3, "first", "test,string");var c = new Array(20);a[1.23] = "test";document.write("a.length = " + a.length + "<br />");document.write("b.length = " + b.length + "<br />");document.write("c.length = " + c.length + "<br />");a[3]="Test";document.write("<br />a[3]="+a[3]);document.write("<br />a.length="+a.length);

Program output:

a.length = 0b.length = 5c.length = 20a[3]=Testa.length=4

The above test also makes it clear that we use an integer as the subscript of the array to actually add an element to the array. Here, the length of the array is used to reflect the mysteries of the javascript array.

By setting the length attribute of the array, the length of the array can be truncated.

A. length = 3; if (a [3] = undefined) {document. write ("<br/> in. length = "+. after length + ", a [3] =" + a [3]);} else {document. write ("<br/> in. length = "+. after length + ", a [3] =" + a [3]);}

Here we test our multi-dimensional array elements. In javascript, multi-dimensional arrays are not actually supported. However, we assign the elements of a one-dimensional array to a one-dimensional array, which looks like a multi-dimensional array is implemented, but in fact, he is still a one-dimensional array, which is the same idea as when we understand arrays in C language, but their implementation mechanism is different.

var g = new Array(3);g[3] = a;g[3][2] = "Test";document.write("<br />g[3][2]="+g[3][2]);

Array join () method:

For (var I = 0; I <20; I ++) {c [I] = I; document. write ("<br/> c [I] =" + c [I]);} document. after the write ("<br/> c element join () method is:" + c. join ());

Array reverse () method:

C. reverse (); document. the result of the write ("<br/> c element after the reverse () method and then join () is:" + c. join ("| "));

Test the concat () method:

var h = new Array(1,2,3);h = h.concat([4,5]);

However, the concat function does not recursively expand an array whose elements are arrays.

h = h.concat(6,7,[9,[10,20]]);document.write("<br />h.length="+h.length+"<br />"+h);document.write("h[8]="+h[8]);

Slice () method: the returned array contains the element specified by the first parameter and the element starting from the element specified by the second parameter, but does not contain the element specified by the second parameter.

document.write("<br>h.slice(4,5)="+h.slice(4,5));document.write("h.slice(5,9)="+h.slice(5,9))

The splice () method is a common method for inserting or deleting array elements. The first parameter of the splice function specifies the position of the element to be inserted or deleted in the array. The second parameter specifies the number of elements to be deleted from the array. After the second parameter, there can be any number of parameters, they specify the elements inserted from the position specified by the first parameter. Move the first element and subsequent elements accordingly.

Document. write ("<br/> h. h after splice (8, 1) is: "+ h. splice (8, 1); // document. write ("<br/> h. h after splice (8, 0, 'A', 'B', 'test') is: "+ h. splice (8, 0, 'A', 'B', 'test'); h. splice (7,0, 'A', 'B', 'test'); document. write ("<br/> h. h after splice (7,0, 'A', 'B', 'test') is: "+ h );

Arrays in javascript are similar to php as stacks, which is interesting and useful. The following are small instances used as stacks:

The push method attaches one or more new elements to the end of the array and returns the new length of the array. Pop will delete the last element of the array, stick to the length of the array, and return the deleted Value.

Var stack = new Array (); stack. push (1, 2); document. write ("<br/> the stack element is:" + stack); document. write ("<br/> stack. length = "+ stack. length); document. write ("<br/> stack. pop () returns: "+ stack. pop (); document. write ("<br/> stack. length = "+ stack. length );

The following are small instances used as Queues:

The unshift method adds one or more elements to the header of the array element, and moves the existing element to the largest position of the subscript to free up space. It returns the new length of the main family.

Shift is to delete and return the first element of the array, and then move all the following elements forward to fill the blank left by the first element.

Var list = []; list. unshift (6, 2); document. write ("<br> the content of the list is:" + list); document. write ("<br/> the shift method of the list is:" + list. shift ());

In addition, the toString () method that we are familiar with in java is left behind:

document.write(c.toString());

To put it bluntly, the toString () method of the array has the same effect as the join () method without parameters.

OK. Let's talk about it here.

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.