The array API in JS

Source: Internet
Author: User
Tags javascript array

3 properties of an array object

1. 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, and when the length property is set to an earlier 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 numbers

alert (arr.length); Displays the length of the array 10

arr.length=12; Increase the length of an array

alert (arr.length); The length of the display array has changed to 12

Alert (arr[8]); Displays the value of the 9th element, which is 56

arr.length=5; Reduces the length of the array to 5, and the elements with an index equal to or more than 5 are discarded

Alert (arr[8]); shows that the 9th element has changed to "undefined"

arr.length=10; Restores the array length to 10

Alert (arr[8]); Although the length is restored to 10, the 9th element cannot be retracted, showing "undefined"

By the above code we can clearly see the property of the length property. But the length object can be not only explicitly set, it may also be implicitly modified. You can use an undeclared variable in JavaScript, or you can use an undefined array element (an element that has an index that exceeds or equals length), at which point the value of the length property is set to the value of the index of the element being used plus 1. For example, the following code:

var arr=[12,23,5,3,25,98,76,54,56,76];//defines an array that contains 10 numbers

alert (arr.length);//Display 10

arr[15]=34;

alert (arr.length);//Display 16

The code also defines a 10-digit array, with the alert statement to see its length as 10. An element with an index of 15 is then assigned a value of 15, or arr[15]=34, and then the length of the array is output with the alert statement, resulting in 16. In any case, this is a surprising feature for developers who are accustomed to strongly typed programming. In fact, the initial length of an array created with the new Array () is 0, and it is the operation that does not define the element in it that 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 capacity of the array. Therefore, an in-depth understanding of the length attribute can be used flexibly in the development process.

2. 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 = This[i];

}

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 6.

3. 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.

For example:

x = new String ("Hi");

if (X.constructor = = String)//Processing (condition is true).

Or

function MyFunc {

function body.

}

y = new MyFunc;

if (Y.constructor = = MyFunc)//Processing (condition is true).

For arrays:

y = new Array ();

8 classifications and multiple methods for array objects

1. Creation of arrays

var arrayobj = new Array ();//Create a default array with a length of 0

var arrayobj = new Array (size);//Create an array of size length, note that the length of the array is variable, so not the upper limit, is the length

var arrayobj = new Array (item1,item2,.....

);//Create an array and assign an initial value

To illustrate, although the second method creates an array that specifies the length, the array is actually variable in all cases, meaning that even if the length is 5, the element can still be stored outside the specified length, note that the length changes accordingly.

2. Access to elements of an array

var arrayitemvalue=arrayobj[1]; Gets the element value of the array

Arrayobj[1]= "To give new values"; Assigning a new value to an array element

3. Adding array elements

Arrayobj.push (item1,item2,...); /Add parameters to the end of the array and return the new length of the array

Cases:

var a = [1,2,3,4,5];

A.push (6,7); A result is: [1,2,3,4,5,6,7]

Arrayobj.unshift (item1,item2,...); /add parameter to array start, elements in array automatically move back, return array new length

Cases:

var a = [1,2,3,4,5];

A.unshift ( -2,-1); A result is: [ -2,-1,1,2,3,4,5]

Arrayobj.splice (start,deletecount,item1,item2,...); /Remove the DeleteCount element from the subscript start position and insert the item1,item2,... from that position, the insertion position and the following element automatically move back, and the method returns the item that was deleted.

Cases:

var a = [1,2,3,4,5];

var B = A.splice (2,2,7,8,9); A result is: [1,2,7,8,9,5],b result is 5:[3,4]

Also note:

var B = a.splice (0,1); Equivalent Shift () method

A.splice (0,0,-2,-1); var B = a.length; Equivalent Unshift () method

var B = A.splice (a.length-1,1); Equivalent Pop () method

A.splice (a.length,0,6,7); Equivalent push () method

4. Deletion of array elements

Arrayobj.pop (); Delete the last item of the original array;

Cases:

var a = [1,2,3,4,5];

A.pop (); A result is: [1,2,3,4]

Arrayobj.shift (); Removes the first element and returns the element value, and the elements in the array are automatically moved forward

Cases:

var a = [1,2,3,4,5];

A.shift (); A result is: [2,3,4,5]

Arrayobj.splice (Start,deletecount); Removes the DeleteCount element from the subscript start position, returning the removed element as an array

Cases:

var a = [1,2,3,4,5];

var B = A.shift (0,2); A result is: [3,4,5],b result is: []

5. Interception and merging of arrays

Arrayobj.slice (Start[,end]); Returns a new array of items from the original array starting from the start subscript to the end-1 subscript, and returns all elements after the start subscript if End is not filled in.

Cases:

var a = [1,2,3,4,5];

var B = A.slice (1,3); A result is: [1,2,3,4,5],b result is: [2,3]

var B = a.slice (1); A result is: [1,2,3,4,5],b result is: [2,3,4,5]

Arrayobj.concat (item1,item2,...); Returns a new array that is composed of parameters added to the original array, and the original array is unchanged

Cases:

var a = [1,2,3,4,5];

var B = A.concat (6,7); A result is: [1,2,3,4,5],b result is: [1,2,3,4,5,6,7]

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 the

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 (); Reverse the array, returning the array address after the reverse order

Cases:

var a = [1,2,3,4,5];

var B = A.reverse (); A result is: [5,4,3,2,1],b result is: [5,4,3,2,1]

Note that this returns the array address, not the copy of the array, and if the original array changes after the reverse order, then the inverse array is arbitrarily changed.

On top of this, do the following:

A.pop (); A result is: [5,4,3,2],b result is: [5,4,3,2]

Or

B.pop (); A result is: [5,4,3,2],b result is: [5,4,3,2]

Arrayobj.sort ([orderfunction]); Returns the sorted array address, sorted by the specified parameter array element

Cases:

Continue to do sort () in the Arrayobj.reverse () example

A.sort (); A result is: [2,3,4,5],b result is: [2,3,4,5]

It is important to note that in a specific array you can sort by the method specified by the Orderfunction parameter.

8. String of array elements

Arrayobj.join (separator); Concatenate the elements of an array together to form a string, separator as a delimiter, or by default with commas as delimiters.

var a = [1,2,3,4,5];

var B = A.join ("|"); A result is: [1,2,3,4,5],b result is: "1|2|3|4|5"

toLocaleString, toString, valueOf: Can be seen as a special use of joins, not commonly used.

Reprint Address

Http://www.cnblogs.com/lynnlin/archive/2009/03/06/1404830.html

The array API in JS

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.