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
neel
5,31312 gold badges50 silver badges67 bronze badges
1 Answer 1
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
Sanketh Katta
6,3712 gold badges33 silver badges31 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
arr = [{ "val": i, "text": i } for i in range(32)]and would produce the same result as above