gpt4 book ai didi

javascript - 如何通过设置一周的第一天来对星期几进行排序?

转载 作者:行者123 更新时间:2023-11-30 07:01:39 24 4
gpt4 key购买 nike

如何排序?例如,我希望我一周的第一天是星期一。从此:

周日周一周二周三周四周五周六

我想要这个:

周一周二周三周四周五周六周日

或者我希望我一周的第一天是星期五

那么输出应该是这样的:

周五周六周日周一周二周三周四

let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] //by default first day of week is 0
let firstDayOfWeek = 1 //Mon

days = days.map(m=>{
//how i can sort this?
return m
})

最佳答案

firstDayOfWeek 之前的日子切片,并将它们添加到数组的末尾:

const orderByFirstDayOfWeek = (firstDayOfWeek, days) =>
[...days.slice(firstDayOfWeek), ...days.slice(0, firstDayOfWeek)]

const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] //by default first day of week is 0

console.log(JSON.stringify(orderByFirstDayOfWeek(1, days))) // Mon 1st
console.log(JSON.stringify(orderByFirstDayOfWeek(5, days))) // Fri 1st

关于javascript - 如何通过设置一周的第一天来对星期几进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56237337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com