其他分享
首页 > 其他分享> > js中splice的删除、截取、插入、替换

js中splice的删除、截取、插入、替换

作者:互联网

var arr =["George","John","Thomas","James","Adrew","Martin"]

let mm = arr.splice(2,3)

console.log(arr)  //  结果  ["George","John","Martin"]    如果arr不赋给某个值,直接使用arr.splice(2,3) ,结果arr就是删除第三位后面的三个元素之后剩余元素组成的数组

console.log(mm)   //  结果  ["Thomas","James","Adrew"]   如果arr赋给某个值,结果mm就是第三位元素后面的三个元素组成的数组。

 

var arr1 =["George","John","Thomas","James","Adrew","Martin"]   

arr1.splice(2,0,"Jack")   [ "George", "John", "Jack", "Thomas", "James", "Adrew", "Martin" ]  // 插入  将新元素插入第三个位置,后面的依次排后

arr1.splice(2,1,"Jack")   [ "George", "John", "Jack", "James", "Adrew", "Martin" ] // 替换    将新元素替换掉第三个元素

 

标签:arr,James,截取,Adrew,js,splice,Martin,John
来源: https://www.cnblogs.com/wwxdxgb/p/14636291.html