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.

General Grievance
5,12039 gold badges40 silver badges60 bronze badges
asked Sep 3, 2013 at 6:23
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 }); 
}
answered Sep 3, 2013 at 6:25
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.