下面的代码是否有更好的方法。我的代码正在运行,但我只是想知道是否有更好的方法。 我有一个数组,我想将该数组中的最后一项返回到第一个位置。
const replce = arr => {
let n = arr.pop();
arr.splice(0, 0, n);
return arr;
};
console.log(replce(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']));
最佳答案
您可以使用 Pop 与 destructing .
let arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
let last = arr.pop()
let final = [last,...arr]
console.log(final)
关于javascript - 将数组中的最后一项返回到第一个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54624869/