复制数组的一部分

slice() 方法返回数组的一部分的副本。

它需要两个参数,arr.slice([begin[, end]])

开始

从零开始的索引,这是提取的开始。

结束

基于零的索引,它是提取的结束,切换到此索引,但不包括在内。

如果结尾是负数,则 end = arr.length + end

例 1

// Let's say we have this Array of Alphabets
var arr = ["a", "b", "c", "d"...];

// I want an Array of the first two Alphabets
var newArr = arr.slice(0, 2); // newArr === ["a", "b"]

例 2

// Let's say we have this Array of Numbers
// and I don't know it's end
var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9...];

// I want to slice this Array starting from
// number 5 to its end
var newArr = arr.slice(4); // newArr === [5, 6, 7, 8, 9...]