Details about JS array attributes and methods

Source: Internet
Author: User
Tags array length arrays

When using javascript, you often need to operate the data (Array). Here we will summarize the methods and attributes of the Array.
I. Array method

1. Create an array

Note the second method: although the length is specified for creating an array, the array actually gets longer in all cases, that is, even if the length is 5, you can still store elements outside the specified length (the array length also changes ).


Var arrayObj = new Array (); // create an Array
Var arrayObj = new Array ([size]); // create an Array and specify the length. Note that the length is not the upper limit.
Var arrayObj = new Array ([element0 [, element1 [,... [, elementN]); // create an Array and assign values

2. Access to array elements

Var testGetArrValue = arrayObj [1]; // Obtain the element value of the array.
ArrayObj [1] = "this is a new value"; // assign a new value to the array element

3. Add array elements

// Add one or more new elements to the end of the array and return the new length of the array.
ArrayObj. push ([item1 [item2 [... [itemN]);
 
// Add one or more new elements to the array. The elements in the array are automatically removed and the new length of the array is returned.
ArrayObj. unshift ([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 "".
ArrayObj. splice (insertPos, 0, [item1 [, item2 [,... [, itemN]);

4. Deletion of array elements

ArrayObj. pop (); // removes the last element and returns the value of this element.
 
ArrayObj. shift (); // removes the first element and returns the element value. The elements in the array are automatically moved forward.
 
// Delete the specified number of deleteCount elements starting from deletePos in the specified position. The removed elements are returned in the array format.
ArrayObj. splice (deletePos, deleteCount );

5. Truncate and merge an array

// Return part of the array in the form of an array. Note that the end element is not included. If the end element is omitted, all elements after start will be copied.
ArrayObj. slice (start, [end]);
 
// Concatenates multiple arrays (or strings, or arrays and strings) into an array and returns a new connected array.
ArrayObj. concat ([item1 [, item2 [,... [, itemN]);

6. Copy an array

ArrayObj. slice (0); // returns the Copy array of the array. Note that it is a new array, not pointing
ArrayObj. concat (); // returns the Copy array of the array. Note that it is a new array, not pointing

7. Sorting of array elements

ArrayObj. reverse (); // returns the array address.
ArrayObj. sort (); // sorts array elements and returns the array address.

8. Concatenate array elements into strings


ArrayObj. join (separator); // returns a string that connects each element value of the array and is separated by separator.

2. Common attributes of arrays
1, length attribute

(1) the length attribute indicates the length of the array, that is, the number of elements.
(2) when the length attribute is set to a greater value, the array increases, and new elements are added to the end of the array. Their values are undefined.


Var arr = [1, 2, 4, 5];
Alert (arr. length); // display the length of the array 5
Arr. length = 10; // increase the length of the array.
Alert (arr. length); // display that the length of the array has changed to 10
Alert (arr); // arr = [1, 2, 3, 4, 5, undefined]

(3) when the length value is smaller than its current value, the array will be truncated and the elements at the end of the array will be lost.


Var arr = [1, 2, 4, 5];
Alert (arr. length); // display the length of the array 5
Arr. length = 2; // reduce the length of the array
Alert (arr. length); // display that the length of the array has changed to 2
Alert (arr); // arr = [1, 2]

2. prototype attributes

The prototype attribute is common to objects and is used to return references to prototype objects. We usually use the prototype attribute to extend a set of basic functions to the class of the object. In this way, the new instance of the object can "inherit" the operation that grants the object prototype.
For example, the following example shows how to add an array object to return the maximum element value in the array.


Function array_max (){
Var I, max = this [0];
For (I = 1; I <this. length; I ++ ){
If (max <this [I]) max = this [I];
   }
Return max;
}
 
Array. prototype. max = array_max;
 
Var x = new Array (1, 2, 3, 4, 5, 6 );
Var y = x. max (); // y = 6

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.