Introduction to js objects: Array object operations

Source: Internet
Author: User

However, prototype is used in the script. in the process of js, we found that some methods are rarely used, but some methods seem to be classic. The script library is constantly increasing... continue to get started with JS and learn about Array today.

First, let's take a look at its definition:

Copy codeThe Code is as follows:
Var arrayObj = new Array ()
Var arrayObj = new Array ([size])
Var arrayObj = new Array ([element0 [, element1 [, [, elementN])

Where: The value of arrayObj isArrayVariable name of the object.
Size because the subscript of the array starts from zero, the subscript of the created element will start from zeroSize-1.
Element0,..., elementN, which will be createdN+ An array whose element length is n + 1. The syntax must contain more than one element.

If you only wantArrayThe constructor passes a parameter, which is a number, it must be an unsigned 32-bit integer (about 4 billion ). The value is the size of the array. If the value is a numerical value but less than 0 or not an integer, a runtime error occurs.

IfArrayThe constructor is a single value and is not a value. SetLengthThe attribute is 1, and the unique element value becomes a single input parameter.

Because JS arrays are parsed arrays, that is, although multiple elements can be allocated to an array, only the elements containing data actually exist. This reduces the amount of memory used by the array.

An Array object has three built-in attributes: constructor, length, and prototype. I will write an article about constructor, prototype, arguments, and other object attributes. Next we will mainly look at some of the built-in methods of Array, which is very important to us, because this stuff is often used.

Concat Method: Concatenate two or more arrays and return a new array. It is worth noting that (reference type) for object parameters copied from the array being connected to the new array, after copying, it still points to the same object, any change in the new array and source array will lead to another change. (Value Type) for values or strings connected to the new array, only the value is copied, changing the value of a group does not affect the value of another array. Example:
Copy codeThe Code is as follows:
Function ConcatArrayDemo (){
Var a, B, c, d;
A = new Array (1, 2, 3 );
B = "JScript ";
C = new Array (42, "VBScript );
D = a. concat (B, c );
// Returns the array [1, 2, 3, "JScript", 42, "VBScript"]
Return (d );
}

Join Method: Convert the items in the array into strings by specific delimiters and return results. The default Delimiter is Comma. For example:
Copy codeThe Code is as follows:
Function JoinDemo (){
Var a, B;
A = new Array (0, 1, 2, 3, 4 );
B = a. join ("-");
// Return value: "0-1-2-3-4"
Return (B );
}

Pop method:Removes the last element from the array and returns the element. If this array is empty, undefined is returned.

 Push method:Add new elements in the order they appear,If one of the parameters is an array, the array will be added to the array as a single element. To merge two or more elements in an array, useConcatMethod.

ReverseMethodSetArrayThe element position in the object is reversed. This method does not create a newArrayObject. If the array is not continuous,ReverseThe method creates an element in the array to fill in the interval in the array. In this way, the values of all created elements are undefined.
Copy codeThe Code is as follows:
Function ReverseDemo (){
Var a, l; // declare the variable.
A = new Array (, 4); // create an Array and assign values.
L = a. reverse (); // reverse the array content.
// Return value: l = [, 0]
Return (l); // an array of returned results.
}

ShiftMethodRemoves the first element from the array and returns the element.

SliceMethodReturnsArrayObject, includingArrayObj.SliceCOPY methodEndThe specified element, but does not include this element. IfStartIs negative.Length + startProcessing, hereLengthIs the length of the array. IfEndIf it is negative, use itLength+EndProcessing, hereLengthIs the length of the array. If omittedEnd, ThenSliceMethod will always be copiedArrayObj. IfEndAppears inStartPreviously, no elements were copied to the new array. Example:
Copy codeThe Code is as follows:
// Except the last element, all elements in myArray are copied to newArray:
NewArray = myArray. slice (0,-1)

The sort method sorts Array objects appropriately. During execution, no new Array objects are created. If a function is provided for the sortfunction parameter, the function must return one of the following values:

Negative value if the first parameter is smaller than the second parameter.
Zero. If the two parameters are equal.
If the first parameter is greater than the second parameter.
Copy codeThe Code is as follows:
Function SortDemo (){
Var a, l; // declare the variable.
A = new Array ("X", "y", "d", "Z", "v", "m", "r ");
L = a. sort (); // sort the array.
Return (l); // return the sorted array.
}

SpliceMethodCan be removed fromStartSpecify the number of elements at the beginning of the position and insert new elements to modifyArrayObj. The returned value is a new element composed of the removed elements.ArrayObject. The format is as follows:
Copy codeThe Code is as follows:
ArrayObj. splice (start, deleteCount, [item1 [, item2 [,... [, itemN])

ToLocaleStringMethodIt will be explained in the Date object. Generally, this method is only returned to the user and not calculated in code.

UnshiftMethodInsert these elements into the beginning of an array, so these elements will appear in the array in order of the parameter sequence. The format is as follows:
Copy codeThe Code is as follows:
ArrayObj. unshift ([item1 [, item2 [,... [, itemN])

ValueOf Method & toString ()Elements of the array are converted into strings separated by commas (,) and connected together. Its operation andArray. toStringAndArray. joinThe method is the same.

By now, all the stuff of the Array object is almost the same. I sorted them out to consolidate my script knowledge. I again declared that many examples were left by our predecessors, if you feel uncomfortable, 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.