Javascript Array object slice () method, arrayslice

Source: Internet
Author: User

Javascript Array object slice () method, arrayslice

The slice () method returns the selected element from an existing array.

Syntax
ArrayObject. slice (start, end)
Start is required. Specifies where to start selection. If it is a negative number, it specifies the position starting from the end of the array.
That is to say,-1 refers to the last element,-2 refers to the second to last element, and so on.
End is optional. Specifies where to end the selection. This parameter is the array subscript at the end of the array fragment. If this parameter is not specified,
The split array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the elements starting from the end of the array.
The slice () method returns a new array containing elements in the arrayObject from start to end (excluding this element.
This method does not modify the array, but returns a child array. If you want to delete an element from an Array, use the method Array. splice ().

Example 1
Create a new array and display the elements selected from it:

<script type="text/javascript">var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr + "<br />")document.write(arr.slice(1) + "<br />")document.write(arr)</script>

Output:

George, John, Thomas
John, Thomas
George, John, Thomas

Example 2
Create a new array and display the elements selected from it:

<script type="text/javascript">var arr = new Array(6)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"arr[3] = "James"arr[4] = "Adrew"arr[5] = "Martin"document.write(arr + "<br />")document.write(arr.slice(2,4) + "<br />")document.write(arr)</script>

Output:

George, John, Thomas, James, Adrew, Martin
Thomas, James
George, John, Thomas, James, Adrew, Martin

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.