Javascript array note

Source: Internet
Author: User

/* <Br/> Javascript is a non-type language, so an array element can have any data type, different elements of the same Array <br/> can have different types. Element settings of the array can contain other arrays, so that you can create a complex array. <br/> in this regard, JavaScript, as a scripting language, is different from the strict object-oriented C ++. C #, Java. higher flexibility. <br/> */<br/>/* <br/> In javascript1.1 and later versions, arrays are created using constructors array () and operators new, <br/> you can use the following three methods to create arrays in javascript. <br/> */<br/> var A = new array (); <br/> var B = new array (5, 4, 3, "first", "test, string "); <br/> var c = new array (20); <br/> A [1, 1.23] = "Test"; <br/> document. write ("<br/> A [1.23] =" + A [1, 1.23]); <br/>/* <br/> I believe that every person who learns JavaScript from a strong programming language will be surprised by the above operations, <br/> float data is also used as an array subscript. In fact, it is not as you think <br/> JavaScript uses negative numbers, floating-point numbers, (or boolean type, object, other values), JavaScript converts it to a string <br/> using the generated string as the property name of the object, instead of defining a new array element <br/> the instance above is actually creating an attribute named "1.23" for. <br/> */<br/> document. write ("<br/>. length = "+. length); <br/> document. write ("<br/> B. length = "+ B. length ); <Br/> document. write ("<br/> C. length = "+ C. length); <br/> A [3] = "test"; <br/> document. write ("<br/> A [3] =" + A [3]); <br/> document. write ("<br/>. length = "+. length); <br/> // 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, <br/> // here, the length of the array is used to reflect the mysteries of the Javascript array. <Br/> // The length of the array can be truncated by setting the Length attribute of the array. <Br/>. length = 3; <br/> if (a [3] = undefined) <br/>{< br/> document. write ("<br/> in. length = "+. after Length + ", a [3] =" + A [3]); <br/>}< br/> else <br/>{< br/> document. write ("<br/> in. length = "+. after Length + ", a [3] =" + A [3]); <br/>}< br/> // here we test our multi-dimensional array elements <br/>/* <br/> in Javascript, multi-dimensional arrays are not supported. <br/> however, we assign the elements of a one-dimensional array to another one-dimensional array, in this way, it looks like a multi-dimensional array, but <br/> it is actually a one-dimensional array, which is the same as the idea when we understand arrays in C language, however, their implementation mechanisms are different. <Br/> */<br/> var G = new array (3); <br/> G [3] =; <br/> G [3] [2] = "test" <br/> document. write ("<br/> G [3] [2] =" + G [3] [2]); <br/> // array join () method <br/> for (VAR I = 0; I <20; I ++) <br/>{< br/> C [I] = I; <br/> document. write ("<br/> C [I] =" + C [I]); <br/>}< br/> document. write ("<br/> after the Element Join () method of C is:" + C. join (); <br/> // reverse () method of the array <br/> C. reverse (); <br/> document. the result of the write ("<br/> C element after the reverse () method is joined () is:" + C. J Oin ("|"); <br/> // test the Concat () method <br/> var H = new array (1, 2, 3 ); <br/> H = H. concat ([]); <br/> // However, the Concat function does not recursively expand an array whose elements are arrays. <Br/> H = H. concat (6, 7, [9, [10, 20]); <br/> document. write ("<br/> H. length = "+ H. length + "<br/>" + H); <br/> document. write ("<br/> H [8] =" + H [8]); </P> <p> // slice () method <br/> document. write ("<br> H. slice (4, 5) = "+ H. slice (4, 5); <br/> document. write ("H. slice (5, 9) = "+ H. slice (5, 9) <br/> // slice () method: the returned array contains the elements specified by the first parameter and the elements starting with the <br/> // element specified by the second parameter, but does not contain the elements specified by the second parameter.. </P> <p> // splice () method <br/> // The splice () method is a common method for inserting or deleting array elements. <Br/>/* <br/> the first parameter of the splice function specifies the position of the element to be inserted or deleted in the array. <Br/> the second parameter specifies the number of elements to be deleted from the array <br/> after the second parameter, you can have any number of parameters, they specify the elements inserted from the position specified by the first parameter. <Br/> move the first and subsequent elements accordingly. <Br/> */<br/> document. write ("<br/> H. h after splice (8, 1) is: "+ H. splice (8, 1); <br/> // document. write ("<br/> H. h after splice (8, 0, 'A', 'B', 'test') is: "+ H. splice (8, 0, 'A', 'B', 'test'); <br/> H. splice (7,0, 'A', 'B', 'test'); <br/> document. write ("<br/> H. h after splice (7,0, 'A', 'B', 'test') is: "+ H ); </P> <p> // arrays in JavaScript are used as stacks and similar to PhP <br/> // This is more interesting and useful. <Br/> // The following is a small instance used as a stack <br/>/* <br/> the push method attaches one or more new elements to the end of the array, then return the new length of the array. <Br/> pop deletes the last element of the array, sticks to the length of the array, and returns the deleted Value. <Br/> */<br/> var stack = new array (); <br/> stack. push (1, 2); <br/> document. write ("<br/> the stack element is:" + stack); <br/> document. write ("<br/> stack. length = "+ stack. length); <br/> document. write ("<br/> stack. pop () returns: "+ stack. pop (); <br/> document. write ("<br/> stack. length = "+ stack. length ); <br/> // The following is a small instance used as a queue <br/>/* <br/> 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 <br/>. Is the new length of the main family. <Br/> 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. <Br/> */<br/> var list = []; <br/> list. unshift (6, 2); <br/> document. write ("<br> the content of the list is:" + list); <br/> document. write ("<br> the shift method of list is:" + list. shift (); <br/> // In addition, The tostring () method we are familiar with in Java <br/> // It's a piece of cake! <Br/> document. write (C. tostring (); <br/> // to put it bluntly, in fact, the tostring () method of the array and the join () method without Parameters () the results are exactly the same <br/> // OK, this's chapter for array, that's all! </P> <p> // join Concatenates the subscripts of the array. Generally, each element of the array is connected into a string. <Br/> var arr = new array ("html", "CSS", "JavaScript", "dom"); <br/> var arr_str = arr. join (); <br/> document. write ("<br/> the content of the list is:" + arr_str); <br/>/* <br/> here, arr is an array or object, after the join method is used, the returned value arr_str is of the string type. <Br/> here we can compare it with the split method. <Br/> */<br/>/* <br/> three attributes of the array object: length, prototype, constructor <br/> 1. Length attribute <br/> 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. <Br/> unlike most other languages, the Length attribute of the Javascript array is variable, which requires special attention. <Br/> 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 increases; <br/> when the Length attribute is set to an hour earlier 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. <Br/> the following example shows how to change the Length attribute: <br/> var arr = [,]; // defines an array containing 10 numbers <br/> alert (ARR. length); // The length of the array is 10 <br/> arr. length = 12; // increase the length of the array <br/> alert (ARR. length); // The length of the displayed array has changed to 12 <br/> alert (ARR); // The value of the 9th elements is displayed, 56 <br/> arr. length = 5; // reduce the length of the array to 5, and elements with an index equal to or greater than 5 are discarded <br/> alert (ARR ); // display that 9th elements have changed to "undefined" <br/> arr. length = 10; // restore the array length to 10 <br/> alert (ARR); // although the length is restored to 10, 9th elements cannot be recovered, show "undefined" <br/> From the code above, we can clearly see the nature of the Length attribute. <Br/> but the length object can be explicitly set and may be implicitly modified. <Br/> an undeclared variable can be used in Javascript. Similarly, an undefined array element (an element whose index exceeds or equal to length) can be used ), <br/> in this case, the value of the Length attribute is set to the value of the used element index plus 1. <Br/> example code: <br/> var arr = [,]; // defines an array containing 10 numbers <br/> alert (ARR. length); // display 10 <br/> arr [15] = 34; <br/> alert (ARR. length); // display 16 <br/> the Code also defines an array containing 10 numbers. The alert statement shows that the length is 10. <Br/> 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, the result is 16. <Br/> In any case, this is a surprising feature for developers who are used to strong-type programming. <Br/> In fact, the initial length of an array created in the form of new array () is 0. It is the operation on undefined elements that changes the length of the array. <Br/> as described above, the Length attribute is so magical that it can be used to conveniently increase or decrease the size of the array. <Br/> therefore, an in-depth understanding of the Length attribute can be used flexibly during development. <Br/> 2. Prototype attributes <br/> prototype attributes <br/> return the reference of the Object Type prototype. The prototype attribute is common to objects. <Br/> objectname. Prototype <br/> the objectname parameter is the name of the object. <Br/> Note: <br/> the prototype attribute provides a set of basic functions of the object class. <Br/> the new instance "inherit" of the object grants the operation of the object prototype. <Br/> for array objects, the following example describes the purpose of the prototype attribute. <Br/> 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. <Br/> function array_max () <br/> {<br/> var I, max = This [0]; <br/> for (I = 1; I <this. length; I ++) <br/>{< br/> If (max <this [I]) <br/> max = This [I]; <br/>}< br/> return Max; <br/>}< br/> array. prototype. max = array_max; <br/> var x = new array (1, 2, 3, 4, 5, 6); <br/> var y = x. max (); <br/> after the code is executed, y saves the maximum value in array X, or 6. <Br/> 3. constructor attribute <br/> indicates the function for creating an object. <Br/> object. constructor // object is the name of an object or function. <Br/> Note: <br/> the constructor attribute is a member of all objects with prototype. <Br/> these include all inherent JScript objects except global and math objects. <Br/> the constructor attribute stores a reference to the function used to construct a specific object instance. <Br/> example: <br/> X = new string ("hi"); <br/> If (X. constructor = string) // process (the condition is true ). <Br/> // or <br/> function myfunc {<br/> // function body. <Br/>}< br/> Y = new myfunc; <br/> If (Y. constructor = myfunc) // process (the condition is true ). <Br/> for Arrays: <br/> Y = new array (); <br/> If (ARR. constructor = array) Alert ("arr is an array"); <br/> eight categories of array objects and multiple methods <br/> 1. array creation <br/> var arrayobj = new array (); // create a default array with a length of 0 <br/> var arrayobj = new array (size ); // create an array with a size length. Note that the array length is variable, so it is not the upper limit. It is the length <br/> var arrayobj = new array (Item1, item2 ,); // create an array and assign an initial value <br/> it must be noted that although the second method creates an array with a specified length, the array actually gets longer in all cases. <Br/> that is to say, even if the length is set to 5, elements can still be stored outside the specified length. Note: The length will change accordingly. <Br/> 2. Access to elements of the array <br/> var arrayitemvalue = arrayobj; // obtain the element value of the array <br/> arrayobj = "to assign a new value "; // assign a new value to the array element <br/> 3. Add an array element <br/> arrayobj. push (Item1, item2 ,...); // Add the parameter to the end of the array and return the new length of the array. <br/> example: <br/> var A = [1, 2, 3, 4, 5]; <br/>. push (6, 7); // The result of A is: [1, 2, 3, 4, 5, 6, 7] <br/> arrayobj. unshift (Item1, item2 ,...); // Add the parameter to the beginning of the array. The elements in the array are automatically removed and the new length of the array is returned. <br/> for (VAR temp in a) {document. write ("<br/>" + A [temp]) ;}< br/> example: <br/> var = [1, 2, 3, 4, 5]; <br/>. unshift (-2,-1); // The result is: [-2,-,] <br/> for (VAR temp in a) {document. write ("<br/>" + A [temp]) ;}< br/> arrayobj. splice (START, deletecount, Item1, item2 ,...); <br/> // Delete the deletecount elements starting from the subscript start, and insert Item1, item2,... from this position ,.... <Br/> // The insert position and the elements after the insertion are automatically moved. The deleted items are returned. <Br/> example: <br/> var A = [1, 2, 3, 4, 5]; <br/> var B =. splice (, 9); // The result is: [,]. The result of B is 5: []. <br/> note: <br/> var B =. splice (); // equivalent to shift () method <br/>. splice (0, 0,-2,-1); var B =. length; // equivalent to the unshift () method <br/> var B =. splice (. length-1, 1); // equivalent to the pop () method <br/>. splice (. length, 7); // equivalent to the push () method <br/> 4. delete an array element <br/> arrayobj. pop (); // Delete the last entry of the original array. <br/> example: <br/> var A = [1, 2, 4, 5]; <br/>. pop (); // The result of A is: [1, 2, 3, 4] <br/> arrayobj. shift (); // remove the first element and return the element value. The elements in the array are automatically moved forward. <br/> example: <br/> var A = [1, 2, 4, 4, 5]; <br/>. shift (); // The result is: [2, 3, 4, 5] <br/> arrayobj. splice (START, deletecount); // Delete the deletecount elements from the subscript start. The method returns the removed elements in an array. <br/> example: <br/> var A = [1, 2, 3, 4, 5]; <br/> var B =. shift (); // The result of A is: [, 5], and the result of B is: [1, 2] <br/> 5. array truncation and merging <br/> arrayobj. slice (start [, end]); <br/> // return the item group between the start subscript and the end-1 subscript in the original array. If the end field is left blank, all elements starting with the start subscript are returned. <Br/> example: <br/> var A = [1, 2, 3, 4, 5]; <br/> var B =. slice (); // a Returns [, 5], and B returns [] <br/> var B =. slice (1); // The result of A is: [1, 2, 3, 4, 5]. The result of B is: [2, 3, 4, 5] <br/> arrayobj. concat (Item1, item2 ,...); // return a new array consisting of parameters added to the original array, while the original array remains unchanged. <br/> example: <br/> var A = [1, 2, 3, 4, 5]; <br/> var B =. concat (6, 7); // The result of A is: [1, 2, 3, 4, 5]. The result of B is: [1, 2, 3, 4, 5, 6, 7] <br/> 6. Copy an array <br/> arrayobj. slice (0); // returns the copy array of the array. Note that it is a new array, not pointing to <br/> arr Ayobj. concat (); // returns the copy array of the array. Note that it is a new array, not pointing to <br/> 7. Sorting of array elements <br/> arrayobj. reverse (); // returns the array address after the reverse order of the array. <br/> example: <br/> var A = [1, 2, 4, 5]; <br/> var B =. reverse (); // The result of A is: [5, 4, 3, 2, 1]. The result of B is: [5, 4, 3, 2, 1] <br/> // note, the returned array address is not a copy of the array. If the original array changes after the reverse order, then the array in reverse order is changed at Will <br/> // perform the following operations on it <br/>. pop (); // A: [5, 4, 3, 2]; B: [5, 4, 3, 2] <br/> // or <br/> B. pop (); // A: [5, 4, 3, 2]; B: [5, 4, 3, 2] <br/> arrayobj. sort ([orde Rfunction]); // sorts by specified parameter array elements and returns the sorted array address. <br/> example: <br/> // In arrayobj. continue sort () in the reverse () Example <br/>. sort (); // A: [2, 3, 4, 5]; B: [2, 3, 4, 5] <br/> note that, in the specific array, you can sort by the method specified by the orderfunction parameter. <Br/> 8. stringized array elements <br/> arrayobj. join (separator); <br/> // Concatenates the elements of the array to form a string. The separator is separator. If this parameter is omitted, the separator is Comma by default. <Br/> var A = [1, 2, 3, 4, 5]; <br/> var B =. join ("|"); // The result of A is: [1, 2, 3, 4, 5]. The result of B is: "1 | 2 | 3 | 4 | 5" <br/> tolocalestring, tostring, valueof: can be considered as a special use of join, not commonly used. <Br/> */

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.