Home >Web Front-end >JS Tutorial >JS Array.slice implementation method of intercepting array_javascript skills
slice definition and usage
The slice() method returns selected elements from an existing array.
Grammar
arrayObject.slice(start,end)
参数 | 描述 |
---|---|
start | 必需。规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置。也就是说,-1 指最后一个元素,-2 指倒数第二个元素,以此类推。 |
end | 可选。规定从何处结束选取。该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从 start 到数组结束的所有元素。如果这个参数是负数,那么它规定的是从数组尾部开始算起的元素。 |
Return value
Returns a new array containing the elements in arrayObject from start to end (exclusive).
Description
Please note that this method does not modify the array, but returns a subarray. If you want to delete a segment of elements from 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 selects 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 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
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>
Okay, actually use array.alice(0,20); to intercept the first 20.