splice方法

splice():增加、删除、替换数组内某一个或几个的值

注意:会改变原始数组

语法splice(index, len, [item])

index:开始下标

len:长度

[item]:新的值

删除:

//删除其实下标为1,长度为1的值
var arr = ['j','o','n'];
arr.splice(1,1);
console.log(arr);
//['j','n']

添加:

//在下标为0的地方添加'x'
var arr = ['j','o','n'];
arr.splice(0,0,'x');
console.log(arr);
//['x','j','o','n']

替换:

//修改开始下标为2,长度为1的值为o
var arr = ['j','o','n'];
arr.splice(2,1,'o');
console.log(arr);        
//['j','o','o'] 

 

posted @ 2021-05-25 11:31  EF果果  阅读(821)  评论(0编辑  收藏  举报