JavaScript array operation summary and properties, method introduction

Source: Internet
Author: User
Tags array length arrays constructor javascript array

  This article mainly introduced the JavaScript array operation summary, for example the array creation, the addition, the deletion, the merging and so on Operation method, simultaneously summarized the attribute and the operation function, the method and so on, needs the friend to be possible to refer to under

One, the operation of the array   1, the creation of the array   code as follows: var arrayobj = new Array (); Creates an array of var arrayobj = new Array ([size]); Create an array and specify the length, note that is not the upper limit, is the length var arrayobj = new Array ([element0[, element1[, ... [, ELEMENTN]]]); /create an array and assign a value   to illustrate, although the second method creates an array that specifies the length, in all cases the array is longer, that is, even if the length is specified to be 5, the element can still be stored outside the specified length, note: the length changes. 2, the elements of the array access to copy code code as follows: Var testgetarrvalue=arrayobj[1]; Gets the element value of the array arrayobj[1]= "This is the new value"; Give the array element a new value of   3, add the code for the array element as follows: Arrayobj. Push ([Item1 [item2 [...] [Itemn]]]); /Adds one or more new elements to the end of the array and returns an array of new length Arrayobj.unshift ([Item1 [item2]. [Itemn]]]); /Adds one or more new elements to the beginning of the array, the elements in the array are automatically moved back, and the new length of the array is returned Arrayobj.splice (insertpos,0,[item1[, item2[, ...). [, Itemn]]]); /inserts one or more new elements into the array at the specified position, and the element at the insertion point is automatically moved back to "".   4, the deletion code of the array element is as follows: Arrayobj.pop (); Removes the last element and returns the element value Arrayobj.shift (); Removes the first element and returns the element value, which automatically moves the element forward arrayobj.splice (Deletepos,deletecount); Deletes the specified number of DeleteCount elements starting at the specified location, deletepos the removed elements   5, the array interception and merge replication code code as follows: Arrayobj.slice (start, [end]); Returns a portion of an array as an array, noting that the end-corresponding element is not included, if the end is omitted to duplicateAfter the start of all elements Arrayobj.concat ([item1[, item2[, ... [, Itemn]]]); Concatenate multiple arrays (or a string, or a mixture of arrays and strings) into an array, return the new concatenated array   6, and the copy code for the array is as follows: Arrayobj.slice (0); Returns an array of copies, noting that a new array is not pointing to Arrayobj.concat (); Returns an array of copies, note that a new array, not the sort code pointing to   7, array elements is as follows: Arrayobj.reverse (); Reverse element (top to last, last row to top), return array address arrayobj.sort (); The array element is sorted, and the arrays address   8, the string of the array elements   code are as follows: Arrayobj.join (separator); Returns a string that connects each element value of an array, separated by a separator in the middle. toLocaleString, toString, valueof: Can be seen as a special use of join, not commonly used     II, array objects 3 properties   1, Length property   Length property to represent the length of the array , which is the number of elements. Because the index of an array always starts with 0, the upper and lower bounds of an array are: 0 and length-1 respectively. Unlike most other languages, the length property of a JavaScript array is variable, which requires special attention. When the length property is set larger, the state of the entire array does not actually change, only the length property is larger, and when the length property is set to the previous hour, the value of the element whose index is greater than or equal to length in the original array is lost. The following is an example of changing the length property:     Code as follows: Var arr=[12,23,5,3,25,98,76,54,56,76]; Defines an array alert (Arr.length) that contains 10 digits; Displays the length of the array arr.length=12; Increase the length of the array alert (ARR.LENGTH); The length of the display array has changed to alert (Arr[8]); Displays the value of the 9th element, which is arr.length=5; Reduce the length of the array to5, the index equal to or more than 5 elements are discarded alert (arr[8]); shows that the 9th element has changed to "undefined" arr.length=10; Restores the array length to alert (arr[8]); Although the length is restored to 10, but the 9th element is not recoverable, display "undefined"   by the above code we can see clearly the nature of the length property. But the length object can be set not only explicitly, it may also be implicitly modified. You can use a variable that is not declared in JavaScript, and you can use an undefined array element (an element whose index exceeds or equal to length), at which point the value of the length property is set to the value plus 1 for the element index used. For example, the following code:   Code is as follows: Var arr=[12,23,5,3,25,98,76,54,56,76]; alert (arr.length); arr[15]=34; alert (arr.length); The   code also defines a 10-digit array, which can be seen with an alert statement of 10. Then the element with index 15 is assigned to 15, or arr[15]=34, and then the length of the array is output by the alert statement, with 16. In any case, this is a surprising feature for developers who are accustomed to strongly typed programming. In fact, an array created with the new Array () has an initial length of 0, and it is an operation that does not define an element in it, which changes the length of the array.   As you can see from the above introduction, the length property is so magical that it makes it easy to increase or decrease the size of the array. Therefore, a thorough understanding of the length attribute is helpful to the flexible application in the development process.   2, prototype property   Returns a reference to the object type prototype. The prototype property is common to object. The     code is as follows: The Objectname.prototype objectname parameter is the name of the object.   Description: Provides a set of basic functions for an object's class using the prototype property. The operation of the new instance of the object, "inherit", gives the object a prototype. For an array object, use the following example to illustrate the purpose of the prototype property. Adds a method to the array object that returns the maximum element value in the array. To do this, declare a function, add it to the Array.prototype, and use it. The   code is as follows:function Array_max ()   {     var i, max = this[0];      for (i = 1; i < This.lengt H i++)      {     if (Max < this[i])      max = this[i];     &NBS P;}      return Max;  }   Array.prototype.max = Array_max;   var x = new Array (1, 2, 3, 4, 5, 6);   var y = X.max ();     After the code executes, Y saves the maximum value in the array x, or says 6.   3, constructor property   represents the function that created the object.   Object.constructor//object is the name of an object or function.   Description: The constructor property is a member of all objects that have prototype. They include all of the JScript intrinsic objects except the Global and Math objects. The constructor property holds a reference to a function that constructs a particular object instance.   For example:   code is as follows: x = new String ("Hi");   if (X.constructor = = String)//For processing (condition is true).     or code as follows: function MyFunc { //functional body.  }   y = new MyFunc;   if (Y.constructor = = MyFunc)//For processing (condition is true).       III. array operations commonly used functions, methods   toString (): Converts an array to a string   tolocalestring (): Converts an array to a string   join (): Converts an array to a symbolic concatenated string   shift (): Moves an element of the array's head out of   unshift (): Inserts an element in the header of the array   pop (): Deletes an element from the tail of the array   push () : Add an element to the end of the array   concat (): Add elements   slice () to the array: Return the part of the array   reverse (): Sort the array in reverse order   sort (): Sort operations on arrays   Splice (): Inserts, deletes, or replaces an array element

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.