JavaScript在数组的指定位置插入元素

使用splice方法,index为插入的位置

array.splice(index, 0, 元素)

例如,["张三","李四","王五"]

在"李四"前面插入,‘张四’

 

array = ["张三","李四","王五"]
(3) ["张三", "李四", "王五"]
array.splice(1,0,"张四")
[]length: 0__proto__: Array(0)
array
(4) ["张三", "张四", "李四", "王五"]
posted @ 2022-05-24 14:19  雪莉06  阅读(494)  评论(0编辑  收藏  举报