JavaScript Object--array

Source: Internet
Author: User
Tags array length

Syntax for creating an Array object
New Array (); new array (size); new Array (element0, element1, ..., ELEMENTN);
Array Object Properties

1) The constructor property returns a reference to the array function that created this object. -------------Object.constructor

For example: Var test=new Array (); =========>>>>>>> Test.constructor=array

2) The Length property can set or return the number of elements in the array. -------------Arrayobject.length

For example: Var test=new Array (3); =========>>>>>>> test.length=3

3) The prototype property gives you the ability to add properties and methods to an object. --------------Object.prototype.name=value

Array Object method

1) concat () connects two or more arrays and returns the result--------------Arrayobject.concat (array1,array2,..., Arrayx); Note: The Arrayx can be either an array or a specific value.

For example: Var a=new array[1,2,3]; var b=new array[4,5];

Document (A.concat (4,5)); =======>1,2,3,4,5

Document (A.concat (b)); =======>1,2,3,4,5

2) Join () is used to put all the elements in the array into a string.  The elements are delimited by the specified delimiter.  --------------Arrayobject.join (separator); Note:separator is optional, omit the argument, and use a comma as the delimiter.

For example: Var a=new array["A", "B", "C"];

Document (A.join ()); =======>a,b,c

3) Pop () is used to delete and return the last element of the array. (Note: This method changes the original array.)  )-------------Arrayobject.pop (); Note: The last element of Arrayobject is deleted, the array length is reduced by 1, and the value of the element it deletes is returned. If the array is already empty, the pop () does not change the array and returns the undefined value

For example: Var a=new array["A", "B", "C"];

Document (A.pop ()); =======>c

Document (a); =======>a,b

Tip: The Shift () method is used to remove the first element of the array and return the value of the first element.

4) push () adds one or more elements to the end of the array and returns the new length. (Note: This method changes the original array.)  )--------------Arrayobject.push (Newelement1,newelement2,..., newelementx); Note: Newelement1 must be a parameter.

For example: Var a=new array["A", "B", "C"];

Document (A.push (' d ')); =======>4

Tip: The Unshift () method adds one or more elements to the beginning of the array and returns the new length.

5) reverse () reverses the order of the elements in the array. (Note: This method changes the original array.) )

6) Slice () can return the selected element from an existing array.  --------------Arrayobject.slice (start,end); Note: The start must parameter, end is an optional parameter, the method does not modify the array, but instead returns a subarray that contains elements from start to end (excluding the element) in the Arrayobject.

For example: Var a=new array["A", "B", "C"];

Document (A.slice (1)); =======>b,c

Document (A.slice); =======>b

7) sort () sorts the elements of the array. (Note: This method changes the original array.) )-----------------Arrayobject.sort (sortby); Note: SortBy is an optional parameter that specifies the sort order, which must be a function. If you call the method without using parameters, the elements in the array are sorted alphabetically.

<script type= "Text/javascript" >function Sortnumber (A, b) {return A-b}var arr = new Array (6) arr[0] = "ten" arr[1] = "5" ARR[2] = "arr[3" = "arr[4" = "arr[5" = "1" document.write (Arr.sort (sortnumber))   =========>>> >>>>>>>>>>>1,5,10,25,40,1000
</script>

8) Splice () adds/deletes items to/from the array, and then returns the deleted items. (Note: This method changes the original array.) )-----------------Arrayobject.splice (index,howmany,item1,..., itemx);

For example: Var a=new array["A", "B", "C"];

add element: Document (A.splice (1,0, "BB")); =======>a,bb,b,c

Delete an element and add an element: document (A.splice, "BB"); =======>a,bb,c

Delete two elements and add an element: Document (A.splice ("BB")); =======>a,bb

9) The ToString () method converts the array to a string and returns the result. ----------------------arrayobject.tostring (); The return value is the same as the string returned by the join () method without parameters.

JavaScript Object--array

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.