js删除或添加元素:splice()
2017-05-24 00:00
302 查看
摘要: 写一个从数组的指定下标开始,删除确定个元素,在网上搜了一下可以用大部分都说是用splice(),但是用的时候发些好些问题,在这里就将一些测试的结果 记录下来,好好总结一下。
Syntax:
array.splice(start);
array.splice(start,deleteCount);
array.splice(start,deleteCount,item1,item2,item3);
start:数组从start索引值开始删除或添加元素,start可以是0;当start>array.length,在MDN上说原数组的长度将设置成start,但是经过测试原数组的长度并没有改变;当start<0,原数组将从数组的最后开始删除|start|个数。
注:splice删除功能是对于原来的数组进行删除,执行splice()方法之后原数组内容发生改变;
deleteCount:整数、非负数,表明原数组需要删除元素的个数,当deleteCount=0,不删除元素;
点deleteCount>0时,原数组将从start索引值开始删除start个元素;当deleteCount参数被省略时,元素将从start索引值开始删除到最后;
item1,item2,item3...:添加到元素的元素,这些元素从start索引值开始添加,当这些元素被省略时,原数组只有删除功能没有添加功能;
注:在数组可能要用到一些功能
1、删除索引值的数组元素,array.splice(start,1);
2、从索引值开始删除数组的n个元素,array.splice(start,n);
3、从索引值开始删除删除数组的元素到末尾,相当于保留索引值前面的数组元素,array.splice(start);
4、改变索引值数组元素的值:array.splice(start,1,item);
5、从索引值开始改变数组中的n个元素:array.splice(start,n,item1,item2,item3...);
6、从索引值开始向数组中天津n个元素:array.splice(start,0,item1,item2,item3...);
7、从数组的末尾开始删除一个元素或多个元素,array.splice(-count);
8、删除数组的最后一个元素,array.pop();
9、删除数组中第一个元素,array.shift();
以上删除方法返回的都是被删除元素的数组
splice()
在MDN上的解释时该方法通过删除已存在的元素或添加新的元素来改变数组的内容,操作之后原数组会发生改变,splice()返回一个有删除元素组成的数组;Syntax:
array.splice(start);
array.splice(start,deleteCount);
array.splice(start,deleteCount,item1,item2,item3);
start:数组从start索引值开始删除或添加元素,start可以是0;当start>array.length,在MDN上说原数组的长度将设置成start,但是经过测试原数组的长度并没有改变;当start<0,原数组将从数组的最后开始删除|start|个数。
var array1 = ["one","two","three","four","five","six"]; console.log(array.splice(0));//["one", "two", "three", "four", "five", "six"] console.log(array);//[] var array1 = ["one","two","three","four","five","six"]; console.log(array.splice(3));//["four", "five", "six"] console.log(array);//["one", "two", "three"] var array2 = ["one","two","three","four","five","six"]; console.log(array.splice(8));//[] console.log(array);//["one","two","three","four","five","six"] var array3 = ["one","two","three","four","five","six"]; console.log(array.splice(-1));//["six"] console.log(array);//["one", "two", "three", "four", "five"] var array4 = ["one","two","three","four","five","six"]; console.log(array.splice(-8));//["one", "two", "three", "four", "five", "six"] console.log(array);//[]
注:splice删除功能是对于原来的数组进行删除,执行splice()方法之后原数组内容发生改变;
deleteCount:整数、非负数,表明原数组需要删除元素的个数,当deleteCount=0,不删除元素;
点deleteCount>0时,原数组将从start索引值开始删除start个元素;当deleteCount参数被省略时,元素将从start索引值开始删除到最后;
var array = ["one","two","three","four","five","six"]; console.log(array.splice(1,2));// ["two", "three"] console.log(array);// ["one", "four", "five", "six"]
item1,item2,item3...:添加到元素的元素,这些元素从start索引值开始添加,当这些元素被省略时,原数组只有删除功能没有添加功能;
var array1 = ["one","two","three","four","five","six"]; array.splice(1,2,"seven","eight"); console.log(array); //["one", "seven", "eight", "four", "five", "six"] var array2= ["one","two","three","four","five","six"]; array.splice(1,0,"seven","eight"); console.log(array); //["one", "seven", "eight", "two", "three", "four", "five", "six"] var array = ["one","two","three","four","five","six"]; array.splice(1,4,"seven","eight"); console.log(array);//["one", "seven", "eight", "six"]
注:在数组可能要用到一些功能
1、删除索引值的数组元素,array.splice(start,1);
2、从索引值开始删除数组的n个元素,array.splice(start,n);
3、从索引值开始删除删除数组的元素到末尾,相当于保留索引值前面的数组元素,array.splice(start);
4、改变索引值数组元素的值:array.splice(start,1,item);
5、从索引值开始改变数组中的n个元素:array.splice(start,n,item1,item2,item3...);
6、从索引值开始向数组中天津n个元素:array.splice(start,0,item1,item2,item3...);
7、从数组的末尾开始删除一个元素或多个元素,array.splice(-count);
8、删除数组的最后一个元素,array.pop();
9、删除数组中第一个元素,array.shift();
以上删除方法返回的都是被删除元素的数组
相关文章推荐
- JS 添加删除元素的实现代码
- JS添加删除元素
- js删除数组中的元素delete和splice的区别
- JS的DOM操作元素示例1--删除添加元素
- js 给json添加新的字段,或者添加一组数据,在JS数组指定位置删除、插入、替换元素
- js动态添加删除表格元素(兼容IE和Firefox)
- 【JS学习笔记】07 添加和删除节点(HTML 元素)
- [JS]应用splice删除多元素时出现的坑
- JS数组array元素的添加和删除方法代码实例
- js删除数组中的元素delete和splice的区别
- js 对html 元素操作,添加删除修改
- Jquery 动态添加删除元素 用js添加的元素无法删除问题
- 原生js实现查找/添加/删除/指定元素的class
- js 删除append标签添加的元素
- js添加删除表格元素
- 体验js中splice()的强大(插入、删除或替换数组的元素)
- ready和onload比较;可以在下面的script里面调用先前script;动态为元素添加事件;浮动广告;js添加删除tr
- 给数组添加一个根据指定下标删除元素的方法、得到0-100的随机数不重复(js)、得到外联样式的css样式值
- JS实现添加,替换,删除节点元素的方法
- js数组添加或删除元素