4 回答
TA贡献1864条经验 获得超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条经验 获得超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条经验 获得超8个赞
const days = (n) => [...weekdays.slice(n-1), ...weekdays.slice(0, n-1)]
这应该做你需要的。一个不错的小 ES6 单线。
TA贡献1866条经验 获得超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))
添加回答
举报
