[英]Day.js - Get array of remaining months in year
I want to get an array of the index of remaining months (0-index), not including current month, in a countdown, this year (or any date for the sake of it).我想在倒计时、今年(或任何日期)中获取剩余月份(0-index)的索引数组,不包括当前月份。 I'm using
lodash
and dayjs
, but I feel my code is a bit hard to understand.我正在使用
lodash
和dayjs
,但我觉得我的代码有点难以理解。
Is there a more "dayjs" way to get what I want?有没有更“dayjs”的方式来获得我想要的东西? I have not found more help in the doc's library or other threads with a similar problem here.
我没有在文档库或其他有类似问题的线程中找到更多帮助。
// All months - Current year's remaining months (0-index), we map in reverse until reaching 0
const yearRemainingMonths = map(range(11 - dayjs().month()), n => 11 - n)
// []
// Let's pretend we are in June, so we'd get
// [11, 10, 9, 8, 7]
There's not much a cleaner way.没有什么更清洁的方法了。 In the end, you have to loop once or to store the predefined array.
最后,您必须循环一次或存储预定义的数组。
import dayjs from 'dayjs'
const months = [0,1,2,3,4,5,6,7,8,9,10,11]
function remainingMonths(month) {
return [...months].splice(month+1).reverse()
}
console.log(remainingMonths(dayjs().month())) // []
console.log(remainingMonths(5)) // june => [ 11,10,9,8,7,6]
console.log(remainingMonths(0)) // [11,10,9,8,7,6,5,4,3,2,1]
for loop
for loop
function remainingMonths(month) {
const remaining = []
for(let i = 11; i > month; i--) {
remaining.push(i)
}
return remaining
}
console.log(remainingMonths(11)) // []
console.log(remainingMonths(5)) // june => [ 11,10,9,8,7,6]
console.log(remainingMonths(0)) // [11,10,9,8,7,6,5,4,3,2,1]
Probably the for
loop is a bit cleaner.可能
for
循环更干净一些。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.