Home  >  Article  >  Web Front-end  >  JavaScript slice() method returns selected elements from an existing array

JavaScript slice() method returns selected elements from an existing array

黄舟
黄舟Original
2017-11-03 13:18:351349browse

Definition and Usage

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

Syntax

arrayObject.slice(start,end)
Parameters Description
start 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.
end 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.

Return value

Returns a new array containing the elements in arrayObject from start to end (excluding this element).

Description

Please note that this method does not modify the array, but returns a subarray. If you want to delete a section of elements in an array, you should use the method Array.splice().

Tips and Notes

Note: You can use negative values ​​to select elements from the tail of an array.

Note: If end is not specified, the slice() method will select all elements from start to the end of the array.

Example

Example 1

In this example, we will create a new array and then 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

In this example, we will create a new array and then display the selected elements 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

This method extracts part of the string and returns a new string.
Syntax

string.slice( beginslice [, endSlice] );

The following are the details of the parameters:

beginSlice: Start extraction from the index position starting from zero

endSlice: End extraction from the index position starting from zero . If omitted, the end of the string extracted from the slice

Note: As a negative exponent, endSlice represents the offset from the end of the string. string.slice(2,-1) extracts the second to last character and the third character of the string.
Return value:

If successful, slicing returns the index of the regular expression within the string. Otherwise, -1 is returned.

Example:

<html>
<head>
<title>JavaScript String slice() Method</title>
</head>
<body>
<script type="text/javascript">
 
var str = "Apples are round, and apples are juicy.";
 
var sliced = str.slice(3, -2);
 
document.write( sliced );
 
</script>
</body>
</html>

This will produce the following results:

les are round, and apples are juic

The above is the detailed content of JavaScript slice() method returns selected elements from an existing array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn