是否有一种方法可以在不覆盖元素的情况下在特定索引处连接数组,而不使用splice方法?我通常看到concats出现在数组的开头或结尾。
下面是使用splice在索引
处连接数组的示例var colors=["red","blue"];
var index=1;
//if i wanted to insert "white" at index 1
colors.splice(index, 0, "white"); //colors = ["red", "white", "blue"]
```
你可以拿Array#copyWithin
const
colors = ["red", "blue"],
index = 1,
value = "white";
colors.length++; // create space for moving
colors.copyWithin(index + 1, index); // move values to right
colors[index] = value; // add new value
console.log(colors); // ["red", "white", "blue"]