Dog*_*oge 4 javascript momentjs ecmascript-6
我正在尝试使用 moment.js 和 ES6 创建一天中间隔 30 分钟的一组小时。
例子:
let hours = ["12:00 AM", "12:30 AM", "1:00 AM", "1:30 AM", ..., "11:30 PM"]
我已经有这个for功能了:
someFunction () {
const items = []
for (let hour = 0; hour < 24; hour++) {
items.push(moment({ hour }).format('h:mm A'))
items.push(moment({ hour, minute: 30 }).format('h:mm A'))
}
return items
}
Run Code Online (Sandbox Code Playgroud)
但我想让它更像 ES6。
我已经走到这一步了:
someFunction () {
let timeSlots = new Array(24).fill().map((acc, index) => {
let items = []
items.push(moment( index ).format('h:mm A'))
items.push(moment({ index, minute: 30 }).format('h:mm A'))
})
return timeSlots
}
Run Code Online (Sandbox Code Playgroud)
但它输出:
["1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", ...]
function someFunction () {
const items = [];
new Array(24).fill().forEach((acc, index) => {
items.push(moment( {hour: index} ).format('h:mm A'));
items.push(moment({ hour: index, minute: 30 }).format('h:mm A'));
})
return items;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8670 次 |
| 最近记录: |