Js Array Operations

Source: Internet
Author: User
1. Create an array 1 vararrayObjnewArray (); Create an array 23 vararrayObjnewArray ([size]); Create an array and specify the length. Note that it is not the upper limit, is 45 vararrayObjnewArray ([element0 [, elem 1, create an Array 1var arrayObj = new Array (); // create an Array 2 3var arrayObj = new Array ([size]); // create an Array and specify the length. Note that it is not the upper limit. The length is 4 5var arrayObj = new Array ([element0 [, element1 [,... [, elementN]); // create an array and assign values. Although the length is specified in the second method, but in fact, in all cases, the array is longer, that is, even if the length is set to 5, the elements can still be stored outside the specified length. Note: The length will change accordingly. 2. Access 1var testGetArrValue = arrayObj [1]; // obtain the element value of the array 2 3 arrayObj [1] = "this is the new value "; // assign a new value to the array element. 3. Add 1arrayObj to the array element. push ([item1 [item2 [... [itemN]); // Add one or more new elements to the end of the array, and return the new length of the array 2 3 arrayobj. unshift ([item1 [item2 [... [itemN]); // adds one or more new elements to the array. The elements in the array are automatically removed and the new length of the array is 4-5 arrayobj. splice (insertPos, 0, [item1 [, item2 [,... [, itemN]); // insert one or more new elements to the specified position of the array. The inserted element is automatically removed and "" is returned "". 4. Delete 1arrayObj from the array element. pop (); // remove the last element and return the value of this element 2 3arrayObj. shift (); // remove the first element and return the element value. The elements in the array are automatically moved forward to 4 5arrayObj. splice (deletePos, deleteCount); // Delete the specified number of deleteCount elements starting from deletePos in the specified position. Return the elements removed in the array Form 5. truncate the array and merge 1arrayObj. slice (start, [end]); // return part of the array in the form of an array. Note that the elements corresponding to the end are not included, if end is omitted, all elements after start 2 3arrayObj will be copied. concat ([item1 [, item2 [,... [, itemN]); // connects multiple arrays (or strings, or arrays and strings) into an array, returns the new connected array 6. Copies of the array 1a. RrayObj. slice (0); // returns the copy array of the array. Note that it is a new array, not pointing to 2 3arrayObj. concat (); // returns the copy array of the array. Note that it is a new array, not pointing to 7. Sorting 1arrayObj of array elements. reverse (); // returns the array address 2 3arrayObj. sort (); // sorts array elements and returns array address 8. The array element is stringized into 1arrayObj. join (separator); // returns a string that connects each element value of the array and is separated by separator. 2 3 toLocaleString, toString, valueOf: can be considered as a special use of join, not commonly used 2. Three attributes of the array object 1. length attribute Length attribute indicates the length of the array, that is, the number of elements. Because the index of an array always starts from 0, the upper and lower limits of an array are: 0 and length-1. Unlike most other languages, the length attribute of the JavaScript array is variable, which requires special attention. When the length attribute is set to a greater value, the status of the entire array does not actually change, except that the length attribute becomes larger. When the length attribute is set to an hour later than the original value, all the values of the elements whose indexes are greater than or equal to the length in the original array are lost. The following example shows how to change the length attribute: 01var arr = [,]; 02 03 // defines an array containing 10 numbers 04 05 alert (arr. length); // display the length of the array 1006. length = 12; // increase the length of the array. 08 09 alert (arr. length); // The length of the displayed array has changed to 1210 11 alert (arr [8]); // The value of 9th elements is 5612 13arr. length = 5; // reduce the length of the array to 5. elements with an index equal to or greater than 5 are discarded by 14 15 alert (arr [8]); // display that 9th elements have changed to "undefined" 16 17arr. length = 10; // restore the array length to 1018 19 alert (arr [8]); // although the length is restored to 10, 9th elements cannot be recovered, show "undefined" The above Code clearly shows the nature of the length attribute. However, the length object can be explicitly set and may be implicitly modified. JavaScript can use an undeclared variable, or an undefined array element (an element whose index exceeds or is equal to length, the value of the length attribute is set to the value of the used element index plus 1. Example code: 1var arr = [,]; 2 3 alert (arr. length); 4 5arr [15] = 34; 6 7 alert (arr. length); The Code also defines an array containing 10 numbers. The alert statement shows that the length is 10. Then, an element with an index of 15 is used and assigned a value of 15, that is, arr [15] = 34. Then, the array length is output using the alert statement, and the result is 16. In any case, this is a surprising feature for developers who are used to strong-type programming. In fact, the initial length of an Array created in the form of new Array () is 0, and the length of the Array changes only when no element is defined. From the above introduction, we can see that the length attribute is so magical that it can be used to conveniently increase or decrease the array capacity. Therefore, an in-depth understanding of the length attribute can be used flexibly in the development process. 2. The prototype property returns a reference to the object type prototype. The prototype attribute is common to objects. The objectName. prototype objectName parameter is the name of the object. Note: The prototype attribute is used to provide a set of basic functions of the object class. The new instance of the object "inherits" the operation that is granted to the object prototype. The following example describes the purpose of the prototype attribute for array objects. Add a method to the array object to return the maximum element value in the array. To do this, declare a function, add it to Array. prototype, and use it. 01 function array_max () 02 {03 var I, 04 max = this [0]; 05 06 for (I = 1; I <this. length; I ++) 07 {08 if (max <this [I]) 09 max = this [I]; 10} 11 return max; 12} 13 14Array. prototype. max = array_max; 15 16var x = new Array (1, 2, 3, 4, 5, 6); 17 18var y = x. max (); after the code is executed, y saves the maximum value in array x, or 6. 3. the constructor attribute indicates the function of the object to be created. Object. constructor // object is the name of an object or function. Note: The constructor attribute is a member of all objects with prototype. They include all inherent JScript objects except Global and Math objects. The constructor attribute stores references to the functions used to construct a specific object instance. Example: 1x = new String ("Hi"); 2 3if (x. constructor = String) // process (the condition is true ). Or 1 function MyFunc {2 3 // function body. 4 5} 6y = new MyFunc; 7 8if (y. constructor = MyFunc) // process (the condition is true ). For arrays: 1y = new Array ();

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.