1 回答
TA贡献1783条经验 获得超4个赞
您可以简单地移动源数组并将其附加到临时数组,然后您可以slice:
const months = [
{ name: "Jan", value: 0, active: false, visible: false },
{ name: "Feb", value: 1, active: false, visible: false },
{ name: "Mar", value: 2, active: false, visible: false },
{ name: "Apr", value: 3, active: false, visible: false },
{ name: "Mei", value: 4, active: false, visible: false },
{ name: "Jun", value: 5, active: false, visible: false },
{ name: "jul", value: 6, active: false, visible: false },
{ name: "Aug", value: 7, active: false, visible: false },
{ name: "Sep", value: 8, active: false, visible: false },
{ name: "Okt", value: 9, active: false, visible: false },
{ name: "Nov", value: 10, active: false, visible: false },
{ name: "Dec", value: 11, active: false, visible: false }
]
const currentMonthIndex = new Date().getMonth()
function getVisibleMonths(months, currentMonthIndex) {
const tmp = [...months]
const res = []
for (i = 0; i < currentMonthIndex; i++) {
res.push(tmp.shift())
}
return [...tmp, ...res].slice(0, 4)
}
console.log(getVisibleMonths(months, currentMonthIndex))
添加回答
举报