数组循环javascript

数组循环javascript,javascript,html,combobox,Javascript,Html,Combobox,是否可以在“年度”中循环,而不是逐个添加{value:'16',text:'2018'},或者循环年份并在当前年份停止,然后在组合框中显示其文本。我的建议和评论如下: var categories = { "None":[{value:'1', text:'No category selected'}], "Monthly":[{value:'2', text:'January'},{value:'3', text:'February'},{value:'4', text:'Ma

是否可以在“年度”中循环,而不是逐个添加
{value:'16',text:'2018'}
,或者循环年份并在当前年份停止,然后在组合框中显示其文本。

我的建议和评论如下:

var categories = {
    "None":[{value:'1', text:'No category selected'}],
    "Monthly":[{value:'2', text:'January'},{value:'3', text:'February'},{value:'4', text:'March'},{value:'5', text:'April'},{value:'6', text:'May'},{value:'7', text:'June'},{value:'8', text:'July'},{value:'9', text:'August'},{value:'10', text:'September'},{value:'11', text:'October'},{value:'12', text:'November'},{value:'13', text:'December'}],
    "Yearly":[{value:'14', text:'2016'},{value:'15', text:'2017'},{value:'16', text:'2018'}],
}
var类别={
“无”:[{value:'1',text:'No category selected'}],
“每月”:[{
值:“2”,
正文:“一月”
},
{
值:“3”,
文字:“二月”
},
{
值:“4”,
文字:“三月”
},
{
值:“5”,
文字:“四月”
},
{
值:“6”,
文字:“五月”
},
{
值:“7”,
文字:“六月”
},
{
值:“8”,
正文:“七月”
},
{
值:“9”,
文字:“八月”
},
{
值:“10”,
正文:“9月”
},
{
值:“11”,
正文:“十月”
},
{
值:“12”,
正文:“11月”
},
{
值:“13”,
正文:“12月”
}],
“每年”:[]
};
函数AddYearlyCategories从给定的EAR和VALUE开始(startYear,startValue){
var currentYear=新日期().getFullYear();
如果(当前年份<起始年份){
return;//退出以避免无休止的循环
}
//断言Yearly为array,否则将设置空数组
categories.Yearly=categories.Yearly | |[];
//下面的循环将对象推送到数组中
//从给定的startValue和StartEar开始
//直到达到当前年份(也将推送)

while(startYear)不是直接生成的,但是您可以在函数中动态生成属性。您是否尝试过找到答案?
var categories = {
    "None": [{value: '1', text: 'No category selected'}],

    "Monthly": [{
        value: '2',
        text: 'January'
    },
        {
            value: '3',
            text: 'February'
        },
        {
            value: '4',
            text: 'March'
        },
        {
            value: '5',
            text: 'April'
        },
        {
            value: '6',
            text: 'May'
        },
        {
            value: '7',
            text: 'June'
        },
        {
            value: '8',
            text: 'July'
        },
        {
            value: '9',
            text: 'August'
        },
        {
            value: '10',
            text: 'September'
        },
        {
            value: '11',
            text: 'October'
        },
        {
            value: '12',
            text: 'November'
        },
        {
            value: '13',
            text: 'December'
        }],

    "Yearly": []
};

function addYearlyCategoriesStartedFromGivenYearAndValue(startYear, startValue) {
    var currentYear = new Date().getFullYear();

    if (currentYear < startYear) {
        return; //exit to avoid endless loop
    }

    // assert that Yearly is array if not will set empty array 
    categories.Yearly = categories.Yearly || [];

    // below loop will push objects to array
    // starting from given startValue and startYear
    // until reaches currentYear (it will also be pushed)
    while (startYear <= currentYear) {
        categories.Yearly.push({
            // startValue++ post incrementation returns current value
            // and increments this variable for next get
            value: startValue++,
            text: startYear++
        });
    }
}

// create categories.Yearly array values
addYearlyCategoriesStartedFromGivenYearAndValue(2016, 14);