Reputation: 2294
I need to dynamically create an array based on a range. I have a req_count variable. My array needs to always have the first 6 spots as null
, and then the variable spots as { "sType": "title-string" }
. For some reason, my code below doesn't seem to be working. Any ideas?
Javascript:
var aoColumns = ['null', 'null', 'null', 'null', 'null', 'null']
for (i=0;i<=req_count;i++){
aoColumns.push('{ "sType": "title-string" }');
}
So if req_count = 5, the result should be:
[
null,
null,
null,
null,
null,
null,
{ "sType": "title-string" },
{ "sType": "title-string" },
{ "sType": "title-string" },
{ "sType": "title-string" },
{ "sType": "title-string" }
],
Upvotes: 1
Views: 928
Reputation: 120318
String is not the only type in javascript ;). 'null'
should be null
and
aoColumns.push('{ "sType": "title-string" }');
should be
aoColumns.push({ "sType": "title-string" });
Upvotes: 1
Reputation: 1189
Remove the quotes from inside the push... Push real objects into it, not strings.
For example:
aoColumns.push({ "sType": "title-string" });
Instead of
aoColumns.push('{ "sType": "title-string" }');
Upvotes: 1
Reputation: 82654
var aoColumns = ['null', 'null', 'null', 'null', 'null', 'null']
should be
var aoColumns = [null, null, null, null, null, null]
and
aoColumns.push('{ "sType": "title-string" }');
should be
aoColumns.push({ "sType": "title-string" });
Upvotes: 2
Reputation: 83376
You're pushing strings, not objects:
Change
for (i=0;i<=req_count;i++){
aoColumns.push('{ "sType": "title-string" }');
}
to
for (i=0;i<=req_count;i++){
aoColumns.push({ "sType": "title-string" });
}
The same goes for your initial null values. You're pushing the string "null" instead of actual null.
Change
var aoColumns = ['null', 'null', 'null', 'null', 'null', 'null']
to
var aoColumns = [null, null, null, null, null, null];
Upvotes: 5