首页 > Web开发 > 详细

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

时间:2021-04-09 13:12:39      阅读:20      评论:0      收藏:0      [点我收藏+]

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" ] // 替换    将新元素替换掉第三个元素

 

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

原文:https://www.cnblogs.com/wwxdxgb/p/14636291.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!