js--数组指定位置添加元素

var nums = ["one", "two", "four"];
nums.insert(2, 'three'); // 注意数组索引, [0,1,2..]
array    // ["one", "two", "three", "four"]
Array.prototype.insert = function (index, item) {
  this.splice(index, 0, item);
};

 

参考:https://blog.csdn.net/renfufei/article/details/38143947?utm_medium=distribute.pc_relevant.none-task-blog-title-2&spm=1001.2101.3001.4242

posted @ 2020-09-11 09:42  九零后大辰辰  阅读(427)  评论(0编辑  收藏  举报