The slice() method returns selected elements from an existing array.
Syntax
arrayObject.slice(start,end)
Description | |
---|---|
Required. Specifies where to start the selection. If negative, it specifies the position from the end of the array. That is, -1 refers to the last element, -2 refers to the second to last element, and so on. | |
Optional. Specifies where the selection ends. This parameter is the array index at the end of the array fragment. If this parameter is not specified, the split array contains all elements from start to the end of the array. If this parameter is negative, it specifies the elements starting from the end of the array. |
Note: If end is not specified, the slice() method will select all elements from start to the end of the array.
Example 1
In this example, we will create a new array and then display the elements selected from it:George,John,Thomas
John,Thomas
George,John,Thomas
Example 2
In this example, we will create a new array and then display the elements selected from it:George, John,Thomas,James,Adrew,Martin
Thomas,James
George,John,Thomas,James,Adrew,Martin
Core code:
The above is the detailed content of Detailed explanation of how to use slice() function to intercept array usage examples in javascript. For more information, please follow other related articles on the PHP Chinese website!