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