📌  相关文章
📜  在索引 javascript 数组中设置元素并创建新数组 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:56.677000             🧑  作者: Mango

代码示例2
const items = [1, 2, 3, 4, 5]

const insert = (arr, index, newItem) => [
  // part of the array before the specified index
  ...arr.slice(0, index),
  // inserted item
  newItem,
  // part of the array after the specified index
  ...arr.slice(index)
]

const result = insert(items, 1, 10)

console.log(result)
// [1, 10, 2, 3, 4, 5]