js基础 数组splice

var arr = [1,2,3,4,5];
var hd = arr.splice(0,3);
console.log(hd);
[ 1, 2, 3 ] console.log(arr);
[ 4, 5 ]

 

 

 

var arr = [1,2,3,4,5];
var hd = arr.splice(0,3,"ss","aa");
console.log(hd);
[ 1, 2, 3 ] console.log(arr);
[ "ss", "aa", 4, 5 ]


----
替换
var arr = [1,2,3,4,5];
var hd = arr.splice(1,1,"tt");
console.log(hd);
[ 2 ]
console.log(arr);
[ 1, "tt", 3, 4, 5 ]

---
增加
var arr = [1,2,3,4,5];
var hd = arr.splice(1,0,"aa","dd");
console.log(hd);
[]
console.log(arr);
[ 1, "aa", "dd", 2, 3, 4, 5 ]

 

上一篇:PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)


下一篇:【js知识】splice()实现数组的添加、删除、替换