JS insert into array at specific index
This question already has an answer here:
Well, thats pretty easy. Assuming you have an array with 5 objects inside and you want to insert a string at index 2 you can simply use javascripts array splice method:
var array = ['foo', 'bar', 1, 2, 3],
insertAtIndex = 2,
stringToBeInserted = 'someString';
// insert string 'someString' into the array at index 2
array.splice( insertAtIndex, 0, stringToBeInserted );
Your result will be now:
['foo', 'bar', 'someString', 1, 2, 3]
FYI: The push() method you used just adds new items to the end of an array (and returns the new length)
链接地址: http://www.djcxy.com/p/29306.html上一篇: 如何在数组的索引0处推入元素
下一篇: JS在特定索引处插入数组