gpt4 book ai didi

javascript - 按工作日对日程安排顺序进行排序(例如星期一、星期二、星期三...)

转载 作者:行者123 更新时间:2023-11-28 03:34:06 27 4
gpt4 key购买 nike

我正在尝试解决我的代码中的问题。我有一个硬编码的状态数据,假设它来自 API 调用。默认情况下,操作时间的 API 数据未正确排序,在我的情况下,星期四首先而不是星期一。我的问题是,每当我映射数据时,我都想按星期几(周一、周二、周三……)对它们进行排序。我这里做了一个沙箱https://codesandbox.io/s/crazy-booth-4gwfi

class App extends Component {
state = {
data: {
name: "Starbucks",
operationHours: {
thursday: { from: "8:00", to: "20:00" },
friday: { from: "8:00", to: "20:00" },
saturday: { from: "8:00", to: "20:00" },
sunday: { from: "8:00", to: "20:00" },
monday: { from: "8:00", to: "20:00" },
tuesday: { from: "8:00", to: "20:00" },
wednesday: { from: "8:00", to: "20:00" }
}
}
};
render() {
return (
<div>
<h2>{this.state.data.name}</h2>
{Object.keys(this.state.data.operationHours).map(dayOfWeek => (
<div key={dayOfWeek} className="day-of-week">
{`${dayOfWeek} - ${moment(
this.state.data.operationHours[dayOfWeek].from,
"HH:mm"
).format("hh:mm A")} -
${moment(
this.state.data.operationHours[dayOfWeek].to,
"HH:mm"
).format("hh:mm A")}`}
</div>
))}
</div>
);
}
}

最佳答案

而不是

{Object.keys(this.state.data.operationHours).map(dayOfWeek => (
<div key={dayOfWeek} className="day-of-week">

获取一个包含已排序日期的数组,例如

{['monday', 'tuesday', 'wednesday'].map(dayOfWeek => (
<div key={dayOfWeek} className="day-of-week">

关于javascript - 按工作日对日程安排顺序进行排序(例如星期一、星期二、星期三...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57901971/

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