原仓库代码

数组中元素位置移动

const arrayMove = require('array-move');
const input = ['a', 'b', 'c'];
const array1 = arrayMove(input, 1, 2);
console.log(array1);
//=> ['a', 'c', 'b']
const array2 = arrayMove(input, -1, 0);
console.log(array2);
//=> ['c', 'a', 'b']
const array3 = arrayMove(input, -2, -3);
console.log(array3);
//=> ['b', 'a', 'c']
原结果:1,2,3,4
result:1,2,3,4

简化版:

function arrayMove(array, from, to) {
const newArray = [...array];
const startIndex = from < 0 ? from + newArray.length : from;
if (startIndex < 0 || startIndex >= newArray.length) return newArray;
newArray.splice(
to < 0 ? newArray.length + to : to,
0,
newArray.splice(from, 1)[0],
);
return newArray;
}