4 回答

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊
const weekDays = ['monday', 'tuesday','wednesday', 'thursday', 'friday', 'saturday', 'sunday']
const days = (n) =>
// Splice will take out everything from where ever you want to start until
// the end of the array and remove that part from the original. So
// weekdays only contains whatever is left. So simply add the rest
// using concat
weekDays.splice(n - 1, weekDays.length - n + 1).concat(weekDays);

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊
let a = function SortByDay(startNum) {
let weekDays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
return weekDays
.slice(startNum - 1)
.concat(weekDays
.slice(0, startNum - 1));
};

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊
const days = (n) => [...weekdays.slice(n-1), ...weekdays.slice(0, n-1)]
這應(yīng)該做你需要的。一個(gè)不錯(cuò)的小 ES6 單線。

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊
使用Array.slice():
function arrangeDays(startNum) {
let days = ['monday', 'tuesday','wednesday', 'thursday', 'friday', 'saturday', 'sunday']
let arrStart = days.slice(startNum-1)
let arrEnd = days.slice(0, startNum-1)
return arrStart.concat(arrEnd)
}
console.log(arrangeDays(3))
console.log(arrangeDays(7))
添加回答
舉報(bào)