JS array object entry-level analysis

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: CopyCode The Code is as follows: var arrayobj = new array ()
VaR arrayobj = new array ([size])
VaR arrayobj = new array ([element0 [, element1 [, [, elementn])

Where: arrayobj is the variable name assigned to the array object.
Size because the subscript of the array starts from zero, the subscript of the created element ranges from zero to size-1.
Element0,..., elementn, which creates an array with n + 1 Elements in length. The syntax must contain more than one element.
If only one parameter is passed to the array constructor and the parameter 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.
If a single value is passed to the array constructor and it is not a value, set the Length attribute to 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.
The array object has three built-in attributes: constructor, length, prototype. I will write an article about the attributes of constructor, prototype, arguments, and other objects. Article . 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 code The Code is as follows: 1 function concatarraydemo (){
2 var a, B, c, d;
3 A = new array (1, 2, 3 );
4 B = "jscript ";
5 c = new array (42, "VBScript );
6 d = A. Concat (B, c );
7 // returns the array [1, 2, 3, "jscript", 42, "VBScript"]
8 return (d );
9}
10

Join method: Convert the items in the array into strings using specific delimiters and return results. The default Delimiter is Comma. For example:Copy codeThe Code is as follows: 1 function joindemo (){
2 var A, B;
3 A = new array (0, 1, 2, 3, 4 );
4 B = A. Join ("-");
5 // return value: "0-1-2-3-4"
6 Return (B );
7}

pop method: removes the last element from the array and returns the element. If this array is empty, undefined is returned.
push method: add these elements in the order they appear. If one of the parameters is an array, the array is added as a single element to the array. To merge two or more elements in an array, use the Concat method.
The Reverse Method reverses the element position in an array object. During execution, this method does not create a new array object. If the array is not continuous, the reverse method creates elements in the array to fill in the interval in the array. In this way, the values of all created elements are undefined. copy Code the code is as follows: 1 function reversedemo () {
2 var A, L; // declare the variable.
3 A = new array (, 4); // create an array and assign values.
4 L = A. Reverse (); // reverse the array content.
5 // return: L = [, 0]
6 Return (l); // returns an array of results.
7}

the shift method removes the first element from the array and returns the element.
the slice method returns an array object, which contains the specified part of arrayobj. The Slice Method is always copied to the end specified element, but this element is not included. If start is negative, it is processed as Length + start. Here, length is the length of the array. If end is negative, it is processed as Length + end. Here, length is the length of the array. If end is omitted, the slice method will always be copied to the end of arrayobj. If end appears before start, no elements are copied to the new array. Example:
// all elements in myarray are copied to newarray except the last element:
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 passed is smaller than the second parameter.
· zero, if two parameters are equal.
· positive value. If the first parameter is greater than the second parameter. copy Code the code is as follows: 1 function sortdemo () {
2 var A, L; // declare the variable.
3 A = new array ("X", "Y", "D", "Z", "V", "M", "R ");
4 L =. sort (); // sort the array.
5 return (l); // return the sorted array.
6}

the splice method can remove a specified number of elements starting from the start position and insert new elements to modify arrayobj. The returned value is a new array object consisting of the removed elements. The format is as follows:
arrayobj. splice (START, deletecount, [Item1 [, item2 [,... [, itemn])
the tolocalestring method is described in the date object. Generally, this method is only returned to the user and is not calculated in code.
the unshift method inserts these elements into the beginning of an array, so these elements appear in the array in order of the parameter sequence. The format is as follows:
arrayobj. unshift ([Item1 [, item2 [,... [, itemn])
the elements of the valueof Method & tostring () array are converted into strings separated by commas (,) and connected together. Its operations are the same as those of array. tostring and array. Join.

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.