[JS object] array object for getting started with JS

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

Let's first look at its definition:

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

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

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: 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 by specific delimiters and return results. The default Delimiter is Comma. For example: 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 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.

1 Function Reversedemo (){
2 VaR A, L; // Declare variables.
3 A =   New Array ( 0 , 1 , 2 , 3 , 4 ); // Create an array and assign values.
4 L = A. Reverse (); // Returns the array content.
5 // Return Value: L = [, 0]
6 Return (L ); // Returns an array of results.
7 }

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:

// Except the last element, all elements in myarray are copied to newarray:

Newarray = Myarray. Slice ( 0 , - 1 )

 

SortMethodSetArrayObjects are sorted properly. During execution, no newArrayObject. IfSortfunctionIf the parameter provides a function, 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.
1 Function Sortdemo (){
2 VaR A, L; // Declare variables.
3 A =   New Array ( " X " , " Y " , " D " , " Z " , " V " , " M " , " R " );
4 L = A. Sort (); // Sort the array.
5 Return (L ); // Returns the sorted array.
6 }

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: Arrayobj. splice (START, deletecount, [Item1 [, item2 [,... [, itemn])

TolocalestringMethodIt will be explained in the date object. Generally, this method is only returned to the user, notCode.

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:

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.