0

I know this is a simple issue, but I am stumped.

I want to do this using a for loop in JavaScript

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

I tried this. 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.

3
  • did you search anything about it ? Commented Sep 3, 2013 at 6:27
  • 3
    He has tried something. It did not work. So i think its a valid question Commented Sep 3, 2013 at 6:28
  • 1
    to be fair, this is a valid line of python: arr = [{ "val": i, "text": i } for i in range(32)] and would produce the same result as above Commented Sep 3, 2013 at 6:38

1 Answer 1

16

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

var arr = [];
for (var i = 0; i < 32; i++) {
    arr.push({ val: i, text: i });        
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.