4 回答

TA貢獻1864條經(jīng)驗 獲得超6個贊
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貢獻1875條經(jīng)驗 獲得超5個贊
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貢獻1725條經(jīng)驗 獲得超8個贊
const days = (n) => [...weekdays.slice(n-1), ...weekdays.slice(0, n-1)]
這應該做你需要的。一個不錯的小 ES6 單線。

TA貢獻1866條經(jīng)驗 獲得超5個贊
使用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))
添加回答
舉報