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:
<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>
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:<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>
George, John,Thomas,James,Adrew,Martin
Thomas,James
George,John,Thomas,James,Adrew,Martin
Core code:
<script type="text/javascript"> //JS Array.slice 截取数组 //在JavaScript中,Array对象的slice(start[,end])方法返回数组从下标[start,end)的部分(不包含下标为end的元素)如果没有指定end参数,则从start开始到数组结尾的部分,slice()方法不改变原数组,如果要删除数组的一部分,可以使用splice()方法。 //参数: //(1)start:开始截取的数组下标,如果start是负数,表明从数组尾部开始计算。 //(2)end:结束截取的数组下标,如果end是负数,表明从数组尾部开始计算。 //例1: var arr = [1,2,3,4,5,6,7,8,9]; // [0,1,2,3,4,5,6,7,8] // [-10,-9,-8,-7,-6,-5,-4,-3,-2,-1] document.writeln(arr.slice(5)); // 输出:6,7,8,9 document.writeln(arr.slice(-5)); // 输出:5,6,7,8,9 document.writeln(arr.slice(0,3)); // 输出:1,2,3 document.writeln(arr.slice(1,2)); // 输出:2 document.writeln(arr.slice(3,-2)); // 输出:4,5,6,7 document.writeln(arr.slice(1,9999)); // 输出:2,3,4,5,6,7,8,9 //================================================================================================== //JS Array.splice(start,delete_count,value,...) 插入、删除、替换数组 //参数: //(1)start:开始插入和(或)删除的数组元素的下标。 //(2)delete_count:结束截取的数组下标,如果end是负数,表明从数组尾部开始计算。 //(3)value,...:要插入数组的元素。 //返回:如果从数组中删除了元素,则返回的是被删除的元素的数组 // //例1: document.write("<hr>"); // var arr = [1,2,3,4,5,6,7,8,9]; document.writeln("arr=" + arr); // 输出:arr=1,2,3,4,5,6,7,8,9 document.writeln("arr.splice(5)=" + arr.splice(5)); // 输出:arr.splice(5)=6,7,8,9 document.writeln("arr=" + arr); // 输出:arr=1,2,3,4,5 document.write("<br>"); // var arr = [1,2,3,4,5,6,7,8,9]; document.writeln("arr=" + arr); // 输出:arr=1,2,3,4,5,6,7,8,9 document.writeln("arr.splice(5,1,99,100)=" + arr.splice(5,1,99,100)); // 输出:arr.splice(5,1,99,100)=6 document.writeln("arr=" + arr); // 输出:arr=1,2,3,4,5,99,100,7,8,9 document.write("<br>"); </script>
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!