What you want is the splice
function on the native array object.
(您想要的是本机数组对象上的splice
函数。)
arr.splice(index, 0, item);
will insert item
into arr
at the specified index (deleting 0
items first, that is, it's just an insert).
(将在指定索引处将item
插入到arr
中(首先删除0
项目,也就是说,它只是一个插入)。)
In this example we will create an array and add an element to it into index 2:
(在此示例中,我们将创建一个数组并将一个元素添加到索引2中:)
var arr = []; arr[0] = "Jani"; arr[1] = "Hege"; arr[2] = "Stale"; arr[3] = "Kai Jim"; arr[4] = "Borge"; console.log(arr.join()); arr.splice(2, 0, "Lene"); console.log(arr.join());
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…