Skip to content

Latest commit

 

History

History
44 lines (31 loc) · 1.28 KB

insert-at.md

File metadata and controls

44 lines (31 loc) · 1.28 KB
标题 标签
insertAt(往数组特定索引中插入元素) array(数组)

改变原始数组以在指定索引之后插入给定值。

  • 使用具有适当索引和删除计数为 0 的 Array.prototype.splice(),分散要插入的给定值。
const insertAt = (arr, i, ...v) => {
  if (!Array.isArray(arr)) {
    return console.error('First argument should be an array!');
  }
  if (typeof i !== 'number' || (i | 0) !== i) {
    return console.error('Second argument should be an inter index!');
  }
  arr.splice(i + 1, 0, ...v);
  return arr;
};

ts 代码如下:

调用方式:

let myArray = [1, 2, 3, 4];
insertAt(myArray, 2, 5); // myArray = [1, 2, 3, 5, 4]

let otherArray = [2, 10];
insertAt(otherArray, 0, 4, 6, 8); // otherArray = [2, 4, 6, 8, 10]

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/insert-at.html"></iframe>