//array.splice(a, b, c);
//第一个参数a表示:操作的开始位置 ,第二个参数b表示:从第a个位置开始向后删除b各个元素,第三个元素表示插入的元素(如果c不存在则表示这个函数是用来删除用的),替换操作如何实现呢,就是删除你想替换的那个,然后再插入,下面给出demo,和操作结果
alert('test delete 5 items in [ '+lang+' ]from pos 1 ');
var insert2 = lang.splice(1, 5);
alert('deleted items ['+insert2+']'); //
alert('leave ['+lang+']'); //"php", "java", "javascript", "111", "222", "333"
lang = ["php", "java", "javascript", "111", "222", "333"];
alert('test insert 1 item c++ in [ '+lang+' ] from pos 1 ');
insert2 = lang.splice(1, 0,'c++');
alert('delete items ['+insert2+']'); //
alert('resultarray ['+lang+']');
lang = ["php", "java", "javascript", "111", "222", "333"];
alert('test replace 1 item java to c++ in [ '+lang+' ] ');
insert2 = lang.splice(1, 1,'c++');
alert('delete items ['+insert2+']'); //
alert('resultarray ['+lang+']');
控制台输出如下操作结果:
test delete 5 items in [ php,java,javascript,111,222,333 ]from pos 1
deleted items [java,javascript,111,222,333]
leave [php]
test insert 1 item c++ in [ php,java,javascript,111,222,333 ] from pos 1
delete items []
resultarray [php,c++,java,javascript,111,222,333]
test replace 1 item java to c++ in [ php,java,javascript,111,222,333 ]
delete items [java]
resultarray [php,c++,javascript,111,222,333]
github:https://github.com/zxzxzxygithub/jssplicetest/tree/master