数组 slice 和 splice
// 返回数组片段 var arr = [1,2,3,4,5,6]; console.log(arr.slice(1,3)); // [2, 3] console.log(arr.slice(1)); // [2, 3, 4, 5, 6] console.log(arr.slice(1,-1)); // [2, 3, 4, 5] console.log(arr.slice(-4, -3)); // [3]
* 原数组不会被修改
var arr = [1,2,3,4,5,6]; // console.log(arr.splice(2,2)); // 从索引2开始删除两个 // console.log(arr); console.log(arr.splice(1,4,7,8,9,0,'a','b')); // 从索引1开始删除4个 console.log(arr); // [1, 7, 8, 9, 0, "a", "b", 6]
* 原数组会被修改