How to use javascript arrays _ javascript tips

Source: Internet
Author: User
Javascript is a non-type language, so an array element can have any data type, and 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.

<Script language = "javascript"> // Author: dongge // Date: // objective: to perform basic operations on an array/* javascript is a non-type language, therefore, the elements of an array can have any data type. different elements of the same array can have different types. Element settings of an array can contain other arrays, in this way, you can create a complex array. in this regard, javascript, as a scripting language, is different from the strict object-oriented c ++. c #, java. higher flexibility. * // ** in javascript1.1 and later versions, arrays are created using the constructor Array () and operator new. 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 Rray (20); a [1.23] = "test"; document. write ("a [1.23] =" + a [1.23]); // 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 when you use a negative number, floating point number, (or Boolean, object, or other values ), javascript will convert it into a string // use the generated string as the property name of the object, instead of defining a new array element, // The above instance is actually creating an attribute named "1.23" for. document. write (". length = "+. length); document. write ("B. length = "+ B. length); document. write ("c. length = "+ c. length); a [3] = "Test"; document. Write ("<br> a [3] =" + a [3]); document. write ("<br>. length = "+. length); // 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. // The length of the array can be truncated by setting the length attribute of the array. 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 element/** the javascript does not actually support multi-dimensional arrays * but we assign the element of a one-dimensional array to it as a one-dimensional array, in this way, it seems that multi-dimensional arrays are implemented, but in fact they are still a one-dimensional array. This is the same as the idea 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 is joined () is:" + c. join ("|"); // test var h = new Array (1, 2, 3) of the concat () method; h = h. concat ([]); // However, the concat function does not recursively expand an array of Elements Array. 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 document. write ("<br> h. slice (4, 5) = "+ h. slice (4, 5); document. write ("h. slice (5, 9) = "+ h. slice (5, 9) // slice () method: the returned array contains the element specified by the first parameter and the element starting with the element specified by the second parameter, but does not contain the element specified by the second parameter. // 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 that 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 serve as stacks and are similar to php. // This is more interesting and useful. // The following is a small instance used as a stack./* The push method attaches one or more new elements to the end of the array, and then 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 is a small instance used as a queue/* The unshift method adds one or more elements to the header of the array element, move 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 list is:" + list. shift (); // In addition, The toString () method we are familiar with in java // It's a piece of cake! Document. write (c. toString (); // to put it bluntly, the toString () method of the Array and the join () method without parameters have the same effect // OK, this's chapter for Array, that's all! Script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.