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

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

array.splice(index, 0, 元素)

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

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

array = ["张三","李四","王五"]
(3) ["张三", "李四", "王五"]
array.splice(1,0,"张四")
[]length: 0__proto__: Array(0)
array
(4) ["张三", "张四", "李四", "王五"]


你可能感兴趣的:(Web前端)