- 示例数组
let testArray = ['a', 'b', 'c', 'd', 'e'];
- 使用length属性截取
testArray.length = 3;
console.info(testArray); // [ "a", "b", "c" ]
- 使用slice截取(推荐,执行快),注意执行后不改变原数组
testArray = testArray.slice(0, 2); // [ "a", "b" ]
- 获取数组最后的元素
let newArray = ['a', 'b', 'c', 'd', 'e'];
newArray.slice(-1); // [ "e" ]