The common methods of JS array--js array

Source: Internet
Author: User
Tags array length javascript array

1. Creation of arrays

var arrayobj=New Array(//create an array  var arrayobj = new array< Span class= "Br0" > ([size]//create an array and specify the length, note not the upper limit, is the length  var arrayobj = new array ([element0][element1],[... ],[elementn])  ; //create an array and assign a value 



var testgetarrvalue=arrayobj[1]//Get the element value of the array arrayobj[1]"New value"//give the array element a new value 


3. Adding array elements


Arrayobj.Push([Item1],[Item2],[. . .],[Itemn]);Adds one or more new elements to the end of the array and returns the new length of the array 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, returning the new length of the array  arrayobj. splice (Insertpos,0,[ Item1],[item2],[... ],[itemn])  ; //inserts one or more new elements into the specified position of the array, the elements of the insertion position are automatically moved back, and the "" is returned. 

4, array element deletion

arrayobj. pop (//removes the last element and returns the element value  arrayobj. shift (//removes the first element and returns the element value, and the elements in the array are automatically moved forward  arrayobj. splice (Deletepos,deletecount//deletes the specified number of Deletepos elements from the specified position deletecount, and returns the removed element as an array                 


5. Interception and merging of arrays


Arrayobj. Slice(start[end])//Returns the part of the array as an array, noting that the end corresponding element is not included, and if omit end copies all elements after start arrayobj.  Concat([Item1[, item2[[, Itemn]]])// Concatenate multiple arrays (which can also be strings, or a mixture of arrays and strings) into an array, returning a new, well-connected array          

6, copy of the array

Arrayobj. Slice(0)//Returns a copy array of the array, note that it is a new array, not a pointer to arrayobj.  Concat()//Returns a copy array of the array, note that it is a new array, not a pointer to the

7. Sorting of array elements

Arrayobj. Reverse()//Invert the element (the first row to the last, the last row to the top), and returns the array address arrayobj.  sort ()//sorting array elements, returning the address of arrays       

8. String of array elements

Arrayobj. Join(separator)//Returns a string that connects each element value of an array together, separated by a separator. 


9. Length Property

The Length property represents the size of the array, which is the number of elements in it. Because the index of an array always starts with 0, the upper and lower bounds of an array are: 0 and length-1.

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 change in fact, only the length property becomes larger;

When the Length property is set earlier than the previous hour, the value of the element whose index is greater than or equal to length in the original array is lost. Here is an example that shows changing the length property:

var arr=[12, 23, 5, 3, 25, 98, 76, 54, 56, 76];Defines an array that contains 10 numbersAlert(arr.)Length);Displays the length of the array of arr.Length=12;Increase the length of an arrayAlert(arr.)Length);The length of the display array has changed to 12Alert(arr[8]);Displays the value of the 9th element, which is the three-arr.Length=5;Reduces the length of the array to 5, and the elements with an index equal to or more than 5 are discardedAlert(arr[8]);shows that the 9th element has changed to "undefined" arr.Length=10;Restores the array length to 10Alert(arr[8]); //Although the length is restored to 10, but the 9th element cannot be retracted, showing "undefined" var arr=[5,3,98,76 , Wu, he, Wu ]; Alert(arr.length); arr[n]=; Alert(arr.length);  The length becomes 16.                 

10. Prototype Properties

Returns a reference to the object type prototype. The prototype property is common to object.

Objectname.prototype

The ObjectName parameter is the name of the object.

Description: Provides a basic set of functions for a class of objects using the prototype property. The new instance of the object "inherits" the action given to the object's prototype.

For array objects, use the following example to illustrate the purpose of the prototype property.

Adds a method to the array object that returns the value of the largest element in the array. To do this, declare a function, add it to array.prototype, and use it.

function Array_max(){var i, Max=This[0];For(I=1; I<This.Length; I++){If(max<this[i])   Max = Span class= "KW1" >this[i];  return max;}  array. prototype. max = array_max; var X = new array (1;  var y = x.max (                


11. Constructor Properties

Represents a function that creates an object.

Object.constructor//object is the name of the object or function.

Description: The constructor property is a member of all objects that have prototype. They include all JScript intrinsic objects except the Global and Math objects.

The constructor property holds a reference to a function that constructs a particular object instance.

New Array(); (y.= = Array//processing (the condition is true). 




   &NBSP;          

JS array--js Array common method collation

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.