js如何让数组左移一格或右移一格

function toLeft([first, ...rest]) {
    return [...rest, first];
}

function toRight(arr) {
    return [arr.pop(), ...arr];
}

const arr = [1, 2, 3, 4, 5];
console.log(arr);
console.log(toLeft(arr));//[2, 3, 4, 5, 1]
console.log(toRight(arr));//[5, 1, 2, 3, 4]

posted on 2022-03-29 11:22  GameCat  阅读(607)  评论(0编辑  收藏  举报

导航