在JS数组指定位置插入元素

// 原来的数组  
var array = ["one", "two", "four"];  
// splice(position, numberOfItemsToRemove, item)  
// 拼接函数(索引位置, 要删除元素的数量, 元素)  
array.splice(2, 0, "three");  
  
array;  // 现在数组是这个样子 ["one", "two", "three", "four"]  
可以将这个方法添加到数组原型(Array prototype)中:
var nums = ["one", "two", "four"];  
nums.insert(2, 'three'); // 注意数组索引, [0,1,2..]  
array   // ["one", "two", "three", "four"] 


你可能感兴趣的:(JS)