javascript学习笔记(五) Array 数组类型介绍(2)


//splice()方法
//插入项,插入时指定3个参数:起始位置、0(要删除的项)、要插入的项
var colors = ["red","green","blue"];
var inserted = colors.splice(1,0,"yellow","orange"); //从位置1开始插入两项
alert(colors); //red,yellow,orange,green,blue
alert(inserted); //空数组

//替换项,删除时指定3个参数:起始位置、要删除的项、要插入的任意项
var colors = ["red","green","blue"];
var replaced = colors.splice(1,1,"black","brown"); //删除一项,插入两项
alert(colors); //red,black,browm,blue
alert(replaced); //green

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdjwyj.html