javascript,将月份和年份分配给对象

javascript,将月份和年份分配给对象,javascript,Javascript,我需要制作月份和年份的键值对 结果应该是 options:[ {key: "May 2020"}, {key:"June 2020"}, {key:"July 2020"}....... , {key:"November 2020"}] 我需要在6个月内显示上述JSON。例如:-现在是四月,所以我需要显示从2020年5月到2020年11月 为了得到未来6个月和一年,我写了下面的方法,但我对如何用JSON显示它们感到困惑 const d = new Date(); const mon

我需要制作月份和年份的键值对

结果应该是

options:[ {key: "May 2020"}, {key:"June 2020"}, {key:"July 2020"}....... , {key:"November 2020"}]
我需要在6个月内显示上述JSON。例如:-现在是四月,所以我需要显示从2020年5月到2020年11月

为了得到未来6个月和一年,我写了下面的方法,但我对如何用JSON显示它们感到困惑

 const d = new Date();

    const monthNames = ["January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December"
    ];


    year = d.getFullYear();
    let arr = [];
    for(let i = 1 ; i < 7 ; i++){
      let s = d.getMonth();
     let l = s + i;
     let key =  monthNames[l] + " " + year ;
      arr.push(key);
    }

    console.log(arr);
const d=新日期();
const monthNames=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”,
“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”
];
year=d.getFullYear();
设arr=[];
for(设i=1;i<7;i++){
设s=d.getMonth();
设l=s+i;
设key=monthNames[l]+“”+年;
arr.push(按键);
}
控制台日志(arr);
需要以对象键格式显示。我得到了arr中的所有值,像这样的

代码的核心行可能是:

monthNames[ monthIndex % 12 ] +" "+ year
你可以这样做

const today=新日期();
让currentMonth=today.getMonth();
让currentYear=today.getFullYear();
const monthNames=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”,
“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”
];
让数组=[];
for(设i=0;i<6;i++){
currentmount++;
如果(当前月份>11){
当前月份%=12;
本年度+=1;
}
array.push({
关键字:`${monthNames[currentMonth]}${currentYear}`
});
}

console.log(数组)@Phil是的,更正了谢谢
 const options = [];
        const monthNames = ["January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December"];

        function retKey(month, year){
            let key = month + " " + year;
            return {key};
        }

        let today = new Date();

        for(let i=1; i<=6; i++){
            let tmp = new Date();
            let month, year;

            tmp.setMonth(today.getMonth()+i);

            month = monthNames[tmp.getMonth()].substring(0,3);
            year = tmp.getFullYear();

            options.push(retKey(month,year));

        }
        console.log(options);
(6) [{…}, {…}, {…}, {…}, {…}, {…}]
0: {key: "May 2020"}
1: {key: "Jun 2020"}
2: {key: "Jul 2020"}
3: {key: "Aug 2020"}
4: {key: "Sep 2020"}
5: {key: "Oct 2020"}