splice方法
splice():增加、删除、替换数组内某一个或几个的值
注意:会改变原始数组
语法:splice(index, len, [item])
index:开始下标
len:长度
[item]:新的值
删除:
//删除其实下标为1,长度为1的值 var arr = ['j','o','n']; arr.splice(1,1); console.log(arr); //['j','n']
添加:
//在下标为0的地方添加'x' var arr = ['j','o','n']; arr.splice(0,0,'x'); console.log(arr); //['x','j','o','n']
替换:
//修改开始下标为2,长度为1的值为o var arr = ['j','o','n']; arr.splice(2,1,'o'); console.log(arr); //['j','o','o']