javascript 在 jquery 中使用 for 循环将值添加到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18585473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:29:33  来源:igfitidea点击:

Add values to array using for loop in jquery

javascriptjquery

提问by neel

i know this is a simple issue. but i stumped.

我知道这是一个简单的问题。但我难住了。

i want to do this using a for loop in jquery

我想在 jquery 中使用 for 循环来做到这一点

      var arr = [
        { val: '1', text: '1' },
        { val: '2', text: '2' },
        { val: '3', text: '3' },
       .........
        { val: '30', text: '30' },
        { val: '31', text: '31' }
       ];

i try this . Actualy i want create a select list which shows all month day

我试试这个。实际上我想创建一个显示所有月日的选择列表

        var arr = [
        for (var i = 0; i < 32; i++) {
             { val: i, text: i },
        }
        ];

This shows error.

这显示错误。

回答by Sanketh Katta

Javascript does not have list comprehensions like that, try this instead:

Javascript 没有这样的列表推导式,试试这个:

var arr = [];
for (var i = 0; i < 32; i++) {
    arr.push({ val: i, text: i });        
}