var arr = [1, 20, 30, 32, 67, 89, 100]
var a = arr.splice(0 , 0, 2)
console.log(arr)
// 结果是 arr = [2 ,1, 20, 30, 32, 67, 89, 100]
删除成员
var arr = [1, 20, 30, 32, 67, 89, 100]
var a = arr.splice(4 , 1, )
console.log(arr)
//结果是 arr = [1, 20, 30, 32, 89, 100]
替换成员
var arr = [1, 20, 30, 32, 67, 89, 100]
var a = arr.splice(2 , 1, 2)
console.log(arr)
//结果是 arr = [1, 20, 2, 32, 67 , 89, 100]
注意事项
splice()属于数组对象的方法,若对象使用splice则会报错:
例子1:
var a = {
name:'小名',
age: 12
}
a.splice()
运行结果:Uncaught TypeError: b.splice is not a function.
---------------如果想操作一个存放对象的数组------我暂时解释不了---------------------------------
例子2:
var a = [
{
name:'小名',
age: 12
}
{
name:'小李',
age: 13
}
]
a.splice()