splice方法

splice() 方法始终都会返回一个数组,该数组中包含从原始数组中删除的项(如果没有删除任何

项,则返回一个空数组)

var colors = ["red", "green", "blue"];
var removed = colors.splice(0,1); // 删除第一项
alert(colors); // green,blue
alert(removed); // red,返回的数组中只包含一项


removed = colors.splice(1, 0, "yellow", "orange"); // 从位置 1 开始插入两项
alert(colors); // green,yellow,orange,blue
alert(removed); // 返回的是一个空数组


removed = colors.splice(1, 1, "red", "purple"); // 插入两项,删除一项
alert(colors); // green,red,purple,orange,blue
alert(removed); // yellow,返回的数组中只包含一项

猜你喜欢

转载自blog.csdn.net/weixin_38049458/article/details/67634790
今日推荐