Reputation: 3521
Function argument is stored in such format
["2015-07-05 00:30", 59]
...
["2015-07-05 01:00", 62]
function generateCSV(timeAndValue) {
object = {}
var zero = 0;
for(i = 0; i < timeAndValue.length; i++){
var value = timeAndValue[i]
var days = moment(value[0]).format('YYYY-MM-DD');
obj[days] += value[1] + ', '
}
}
In the loop object takes days as parameter and the amount of value spent on that day. 2015-07-05:
"2015-07-05: "undefined59, 62, 65...
2015-07-06: "undefined61, 61, 60..."
Every parameter value is beginning with "undefined". How can I loop that parameters wouldn't begin with "undefined"?
Upvotes: 0
Views: 41
Reputation: 382150
Replace
obj[days] += value[1] + ', '
with
if (obj[days]===undefined) obj[days] = '';
obj[days] += value[1] + ', '
so that you don't add a string to undefined
.
But instead of adding ", "
after every values, you could also build an array in your loop:
if (obj[days]===undefined) obj[days] = [];
obj[days].push(value[1])
and then use join at the end to avoid the trailing comma (and on the general principle that string building should be deferred to the rendering time).
Be careful to variable declarations: object
might be OK, assuming it's defined in a just outside scope, but the missing declaration of i
is very dangerous, as it's probable it's not the only i
in your application.
Edit: as seen by James, you have a typo, it's either "object" or "obj".
Upvotes: 1