英文:
How to generate array of string with the weekdays from the current weekday using moment
问题
想要生成包含星期几作为数组值的字符串动态数组,用于我正在使用的列表。
要求:=> 如果当前是星期一,则数组应为
['today','tomorrow','Wednesday','Thursday','Friday','Saturday','Sunday']
数组应根据当前日期动态生成。
英文:
Want to generate the dynamic array of string with the weekdays as a value inside the array for the list which I am using.
Required:=> If current weekday is Monday then array should be
['today','tomorrow','Wednesday','Thursday','Friday','Saturday','Sunday']
Array should be dynamic based on the current day.
答案1
得分: 0
以下是代码的翻译部分:
生成包含星期几的字符串的数组的代码如下。
const daysArray = () => {
const weekArray = [...Array(7 - moment().weekday())];
const today = new Date();
return weekArray.map((week, index) => {
const dayName = moment(today)
.add(index, "days")
.calendar()
.split(" at")[0];
const date = moment(today).add(index, "days").format(DATE_FORMAT);
return { value: dayName, name: dayName, date };
});
};
这将生成包含动态星期几的对象数组。如果只返回 dayName,则会得到所期望的结果。
英文:
To generate array with the weekday as a string here is the code.
const daysArray = () => {
const weekArray = [...Array(7 - moment().weekday())];
const today = new Date();
return weekArray.map((week, index) => {
const dayName = moment(today)
.add(index, "days")
.calendar()
.split(" at")[0];
const date = moment(today).add(index, "days").format(DATE_FORMAT);
return { value: dayName, name: dayName, date };
});
};
This will generate the array of object with dynamic weekdays inside it. If you will return only dayName then you'll get you're desired result.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论