admin管理员组

文章数量:1418342

I want to dynamically build an array that will eventually look like this

var data2 = [
    [gd(2013, 5, "23"), 33], [gd(2013, 5, 24), 41], [gd(2013, 5, 25), 29], [gd(2013, 5, 26), 12], [gd(2013, 5, 28), 17]
];

The rest of the code is very hacky from this point onwards as I want it to work before actually making it streamlined...

I build my string like it is above

varData = '[gd(' + thisYear + ',' + thisMonth + ',' + thisDay + '),' + thisInt + ']';

Then I push it to the array data2

data2.push(varData);

This created the array that outputs this -

["[gd(2013,23,5),4]", "[gd(2013,24,5),41]", "[gd(2013,28,5),4]"]

How would I get that same array without the "'s around each index? Or am I going about this in COMPLETELY the wrong way? I don't have too much experience with using arrays in this way, so I'm not sure if it could be built dynamically, or even where to begin.

Edit/Update: The missing gd function!!

function gd(year, month, day) {
    return new Date(year, month - 1, day).getTime();
}

I want to dynamically build an array that will eventually look like this

var data2 = [
    [gd(2013, 5, "23"), 33], [gd(2013, 5, 24), 41], [gd(2013, 5, 25), 29], [gd(2013, 5, 26), 12], [gd(2013, 5, 28), 17]
];

The rest of the code is very hacky from this point onwards as I want it to work before actually making it streamlined...

I build my string like it is above

varData = '[gd(' + thisYear + ',' + thisMonth + ',' + thisDay + '),' + thisInt + ']';

Then I push it to the array data2

data2.push(varData);

This created the array that outputs this -

["[gd(2013,23,5),4]", "[gd(2013,24,5),41]", "[gd(2013,28,5),4]"]

How would I get that same array without the "'s around each index? Or am I going about this in COMPLETELY the wrong way? I don't have too much experience with using arrays in this way, so I'm not sure if it could be built dynamically, or even where to begin.

Edit/Update: The missing gd function!!

function gd(year, month, day) {
    return new Date(year, month - 1, day).getTime();
}
Share Improve this question edited May 28, 2013 at 14:29 Gary Kenyon asked May 28, 2013 at 13:39 Gary KenyonGary Kenyon 3681 gold badge6 silver badges20 bronze badges 6
  • what kind of expression is gd()? – Thomas Junk Commented May 28, 2013 at 13:41
  • 7 You're almost certainly going about this the wrong way. What is gd? – Dark Falcon Commented May 28, 2013 at 13:41
  • gd(2013, 5, "23") is not a valid value, it should be a string – Arun P Johny Commented May 28, 2013 at 13:46
  • So you want it to appear when outputted by the console to not show the " in there (which wouldn't make much sense) or do you actually want that to be an array? Also do you want the gd(...), thisInt bit stored as a string or as 2 actual elements? – Qantas 94 Heavy Commented May 28, 2013 at 13:51
  • 2 XY Problem? – user1106925 Commented May 28, 2013 at 13:52
 |  Show 1 more ment

2 Answers 2

Reset to default 4

If you really want exactly what you have in the first example, then you would build that like this:

var data2 = [];

// For each item:
data2.push([gd(thisYear, thisMonth, thisDay), thisInt]);

Just push the array directly and not in form of a string:

data2.push( [ 'gd(' + thisYear + ',' + thisMonth + ',' + thisDay + ')', thisInt ] );

本文标签: javascriptPush to array without Quotesor remove quotes from arrayStack Overflow