语法格式:splice(index, len, [item])
可以用来替换/删除/添加数组内某一个值或几个值,该方法会改变初始数组。
- index:数组开始下标
- len:替换/删除的长度
- item:替换的值,为删除时item为空
删除:
let arr = ['1','2','3','4']; arr.splice(0,2); console.log(arr.toString()); //3,4
替换:
let arr = ['1','2','3','4']; arr.splice(0,2,['5','6','7']); console.log(arr.toString()); //5,6,7,3,4
新增:
let arr = ['1','2','3','4']; arr.splice(0,0,['5','6','7']); console.log(arr.toString()); //5,6,7,1,2,3,4,