JS object of JS entry of the Array object operation summary _javascript skills

Source: Internet
Author: User
Tags numeric value
However, the process of learning script Prototype.js found that some of the methods we rarely use, but some methods seem to be very classic, the script library is constantly increasing, the front of a ... Continue our JS primer, today to learn about array arrays.

First, let's look at its definition:

Copy Code code as follows:

var arrayobj = new Array ()
var arrayobj = new Array ([size])
var arrayobj = new Array ([element0[, element1[, [, ELEMENTN]]])

Where: Arrayobj is assigned to ArrayThe variable name of the object.
Size because the subscript of an array is zero-based, the subscript of the created element will be from zero to size-1.
element0,..., Elementn This creates an array of n + 1 with an element of n + 1. You must have more than one element when you use this syntax.

If you pass only one parameter to the Array 's constructor, and the argument is a number, it must be an unsigned 32-bit integer (approximately 4 billion). The value becomes the size of the array. A run-time error occurs if the value is numeric, but is less than 0 or is not an integer.

If a single value is passed to the Array constructor and is not a number, the length property is set to 1, and the unique element value becomes a single passed-in parameter.

Because the JS array is an analytic array, that is, although you can assign multiple elements to an array, only the elements that contain the data exist. This reduces the amount of memory used by the array.

The array object has three built-in properties: Constructor,length,prototype I will write a special article on constructor, Prototype,arguments and other object properties, here is not to say more. Here we look at some of the methods built into the array, which is important to us because it is often used.

Concat Method: Joins two or more arrays and returns a new array. It is worth noting that (reference type) for object parameters copied from the array being connected to the new array. Copying still points to the same object, regardless of which of the new array and source array changes, will cause another change; (value type) copy only its value for a numeric value or string that is attached to the new array. A change in the value of an array does not affect the value in the other array. Example:

Copy Code code as follows:

function Concatarraydemo () {
var A, B, C, D;
A = new Array (1,2,3);
b = "JScript";
c = new Array ("VBScript");
D = A.concat (b, c);
Returns an array of [1, 2, 3, JScript, "," VBScript "]
return (d);
}

Join method: Converts an item in an array to a string with a specific delimiter and returns the default delimiter as a comma, as an example:
Copy Code code as follows:

function Joindemo () {
var a, B;
A = new Array (0,1,2,3,4);
b = A.join ("-");
Back: "0-1-2-3-4"
return (b);
}

Pop method:Moves the last element in the divisor group and returns the element. Returns undefined if the array is empty.

Push method:These elements will be added in the order in which the new elements appear,If one of the arguments is an array, the array is added as a single element to the arrays. If you want to combine elements from two or more arrays, use the concatMethod.

Reverse method to reverse the position of an element in an Array object. During execution, this method does not create a new Array object. If the array is not contiguous, thereverse method creates an element in the array to populate the interval in the array. The value of all the elements created by this is undefined.

Copy Code code as follows:

function Reversedemo () {
var a, l; Declare a variable.
A = new Array (0,1,2,3,4); Creates an array and assigns a value.
L = A.reverse (); Reverses the contents of an array.
Back: l=[4,3,2,1,0]
return (L); Returns an array of results.
}

ShiftMethodYou can move the first element in the divisor group and return the element.

SliceMethodReturns a ArrayObject that contains the arrayobjThe specified section. SliceMethod has been copied to the EndThe specified element, but does not include the element. If StartAs a negative, it is used as length + startProcessing, here lengthThe length of the array. If Endis negative, it is treated as length+ EndProcessing, here lengthThe length of the array. If you omit EndSo Slicemethod is copied to the arrayobj's End. If Endappear in StartBefore any elements are copied into the new array. Example:
Copy Code code as follows:

In addition to the last element, all elements in the myarray are copied to the NewArray:
NewArray = Myarray.slice (0,-1)

The sort method sorts the array object appropriately, and does not create a new array object during execution. If you provide a function for the sortfunction parameter, the function must return one of the following values:

Negative value if the first argument passed is smaller than the second argument.
0 if two parameters are equal.
Positive value if the first argument is larger than the second argument.
Copy Code code as follows:

function Sortdemo () {
var a, l; Declare a variable.
A = new Array ("X", "Y", "D", "Z", "V", "M", "R");
L = A.sort (); The sort array.
return (L); Returns the sorted array.
}

SpliceMethodCan remove from StartThe specified number of elements at the beginning of the position and inserting the new element to modify arrayobj。 The return value is a newly-formed element of the removed ArrayObject. The format is as follows:
Copy Code code as follows:

Arrayobj.splice (Start, DeleteCount, [item1[, item2[, ...) [, Itemn]]]

tolocalestring   Method   will be explained in the Date object, typically this method is simply returned to the user and not evaluated as code. The

   unshift  method inserts these elements into the beginning of an array, so these elements appear in the array in the order in which they are in the argument sequence. The format is as follows:  
copy code code as follows:

Arrayobj.unshift [item1[, item2 [, ... [, Itemn]]] The elements of the

   valueof Method &tostring ()   array are converted to strings, separated by commas, and concatenated together. The operation is the same as the   array.tostring   and   array.join   method. &NBSP

  Here are all the things about the array object, I put them out one is to be able to consolidate my knowledge of the script, again a lot of examples are left behind, if you have uncomfortable feeling please forgive me!

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.