数组指定位置插入元素
const arr = [1, 2, 4, 5];
const start = 2;
const num = 3;
arr.splice(start , 0, num);
console.log(arr); // [1, 2, 3, 4, 5]
数组元素位置移动
const arr = [1, 2, 3, 4, 5];
const start = 2;
const i = 4
arr.splice(start, 0, arr[i]);
if (start > i) {
arr.splice(i, 1);
} else {
arr.splice(i + 1, 1);
}
console.log(arr); // [1, 2, 5, 3, 4]
网友评论