Introduction to js objects-Array object operations-javascript tips

Source: Internet
Author: User
One object every day. Today, we also come to a new object. I have never studied JS in a system, but I have never written a very good class library. I have never used a very strong class library, prototype. js is ongoing, and we are moving forward slowly. I believe prototype can be applied in the near future. js to develop your own applications. 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:

The 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 is ArrayVariable name of the object.
Size because the subscript of the array starts from zero, the subscript of the created element will start from zero Size-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:

The 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:

The 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, use ConcatMethod.

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.

The 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.
}


Shift MethodRemoves the first element from the array and returns the element.

Slice MethodReturns ArrayObject, including ArrayObj. SliceCOPY method EndThe specified element, but does not include this element. If StartIs negative. Length + startProcessing, here LengthIs the length of the array. If EndIf it is negative, use it Length+ EndProcessing, here LengthIs the length of the array. If omitted End, Then SliceMethod will always be copied ArrayObj. If EndAppears in StartPreviously, no elements were copied to the new array. Example:

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

The 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.
}


Splice MethodCan be removed from StartSpecify the number of elements at the beginning of the position and insert new elements to modify ArrayObj. The returned value is a new element composed of the removed elements. ArrayObject. The format is as follows:

The Code is as follows:


ArrayObj. splice (start, deleteCount, [item1 [, item2 [,... [, itemN])


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

Unshift MethodInsert 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:

The 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 and Array. toStringAnd Array. 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!
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.