let year = new Date().getFullYear() let month = new Date().getMonth() + 1; let time = '' let itemObj={}; let timeArr=[]; for (let i = 1; i <= 12; i++) { time = `${year}-${month}`; if (month >= 12) { year++; month = 1; } else { month++; } itemObj={text:time}; timeArr.push(itemObj); } console.log(JSON.stringify(timeArr));
输出:
[{"text":"2021-9"},{"text":"2021-10"},{"text":"2021-11"},{"text":"2021-12"},{"text":"2022-1"},{"text":"2022-2"},{"text":"2022-3"},{"text":"2022-4"},{"text":"2022-5"},{"text":"2022-6"},{"text":"2022-7"},{"text":"2022-8"}]