javascript - Result of loop is undefined -
function argument stored in such format
["2015-07-05 00:30", 59] ... ["2015-07-05 01:00", 62] function generatecsv(timeandvalue) { object = {} var 0 = 0; for(i = 0; < timeandvalue.length; i++){ var value = timeandvalue[i] var days = moment(value[0]).format('yyyy-mm-dd'); obj[days] += value[1] + ', ' } } in loop object takes days parameter , amount of value spent on day. 2015-07-05:
"2015-07-05: "undefined59, 62, 65...
2015-07-06: "undefined61, 61, 60..."
every parameter value beginning "undefined". how can loop parameters wouldn't begin "undefined"?
replace
obj[days] += value[1] + ', ' with
if (obj[days]===undefined) obj[days] = ''; obj[days] += value[1] + ', ' so don't add string undefined.
but instead of adding ", " after every values, build array in loop:
if (obj[days]===undefined) obj[days] = []; obj[days].push(value[1]) and use join @ end avoid trailing comma (and on general principle string building should deferred rendering time).
be careful variable declarations: object might ok, assuming it's defined in outside scope, missing declaration of i dangerous, it's probable it's not i in application.
edit: seen james, have typo, it's either "object" or "obj".
Comments
Post a Comment