文章目录
1、关于splice方法说明
splice方法
1、参数说明
1、start 开始索引
2、deleteCount 删除个数
2、方法说明
1、返回值是被删除的元素
2、Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
3、删除元素,如果需要的话,也可以在其他位置插入一些元素,返回被删除的元素
3、类似修改操作
2、案例演示
var array = ["刘备", "关羽", "张飞", "马超", "赵云", "黄忠"];
/* *
* splice方法
* 1、参数说明
* 1、start 开始索引
* 2、deleteCount 删除个数
* 2、方法说明
* 1、返回值是被删除的元素
* 2、Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
* 3、删除元素,如果需要的话,也可以在其他位置插入一些元素,返回被删除的元素
* 3、类似修改操作
*/
// 说明:删除操作
// console.log(array.splice(0, 1)); // 刘备
// 说明:添加操作
// array.splice(0,0,"马梦琪","诸葛村夫");
// '马梦琪', '诸葛村夫',
// '刘备', '关羽',
// '张飞', '马超',
// '赵云', '黄忠'
// console.log(array);
// 说明:类似于修改 其实是先删除,后添加
array.splice(0,1,"孙二娘");
console.log(array);
// 前:[ '刘备', '关羽', '张飞', '马超', '赵云', '黄忠' ]
// 后:[ '孙二娘', '关羽', '张飞', '马超', '赵云', '黄忠' ]