当前位置: 首页 > 工具软件 > spin.js > 使用案例 >

JS的splice

叶德本
2023-12-01

用来替换删除数组元素的 

// 添加
// 为0时不删除项目直接添加数组项,不为0时会删除指定位置指定个数的项目然后再添加新项目
var spliceArr0 = ["aaa","bbb"];
var spliceArr1 = spliceArr0.splice(1,0,"xx");
结果:spliceArr0: [aaa,xx,bbb]; spliceArr1: []
 
 
// 删除数组项,返回值为删除的项目
var spliceArr3 = ["aaa","bbb","ccc","111","22","333"];
var spliceArr4 = spliceArr3.splice(2,2);
结果:spliceArr3: [aaa,bbb,22,333]; spliceArr4: [ccc,111]
 
 
 
// 删除并添加项目,返回值为删除的项目
var spliceArr5 = ["aaa","bbb","ccc"];
var spliceArr6 = spliceArr5.splice(2,1,"xxx");
结果:spliceArr5: [aaa,bbb,xxx]; spliceArr6: [ccc]

 

 类似资料: