js删除数组里的某个元素
//删除起始下标为1,长度为1的一个值(len设置1,如果为0,则数组不变)
删除起始下标为1,长度为1的一个值(len设置1,如果为0,则数组不变)
var arr = ['a','b','c','d'];
arr = ['a','b','c','d'];
arr.splice(1,1);
<span style="color:#aa5500">//['a','c','d']; </span>
<span style="color:#aa5500">//删除起始下标为1,长度为2的一个值(len设置2)</span>
var arr2 = ['a','b','c','d']
arr2 = ['a','b','c','d']
arr2.splice(1,2);
console.log(arr2);
<span style="color:#aa5500">//['a','d']</span>
替换
<span style="color:#aa5500">//替换起始下标为1,长度为1的一个值为‘ttt’,len设置的1</span>
var arr = ['a','b','c','d'];
arr = ['a','b','c','d'];
arr.splice(1,1,'ttt');
<span style="color:#aa5500">//['a','ttt','c','d'] </span>
var arr2 = ['a','b','c','d'];
arr2 = ['a','b','c','d'];
arr2.splice(1,2,'ttt');
console.log(arr2);
<span style="color:#aa5500">//['a','ttt','d'] 替换起始下标为1,长度为2的两个值为‘ttt’,len设置的1</span>
添加 ---- len设置为0,item为添加的值:
var arr = ['a','b','c','d'];
arr = ['a','b','c','d'];
arr.splice(1,0,'ttt');
<span style="color:#aa5500">//['a','ttt','b','c','d'] 表示在下标为1处添加一项'ttt'</span>