Let's talk JSON:
var datat= {"Model": "Model A",
"Datase": [
{
"Id": "DatchikSveta11",
"Group": 2,
"State": "on",
"Data": [{
"Date":"2017-08-11 15:10:34.363",
"Value":"0"
},{
"Date":"2017-08-12 21:12:34.363",
"Value":"32"
},{
"Date":"2017-08-15 21:55:34.363",
"Value":"200"
}],
"DataVolume": "luxs"
},{
"Id": "DatchikSveta2",
"Group": 2,
"State": "on",
"Data": [{
"Date":"2017-08-11 17:11:34.363",
"Value":"100"
},{
"Date":"2017-08-15 18:11:34.363",
"Value":"100"
},{
"Date":"2017-08-16 19:12:34.363",
"Value":"200"
}],
"DataVolume": "luxs"
}
]}
I've got a function that knows how to deal with JSON objects.
parseDate = d3.timeParse("%Y-%m-%d %H:%M:%S.%L");
datat.Datase.forEach(function (kv) {
kv.Data.forEach(function (d) {
parseDate(d.Date);
parseInt(d.Value);
});
});
The tricky part is, this function alters the original JSON object by changing dates to parsed dates. This causes issues if the function is called again because it tries to re-parse already parsed dates.
I'm looking for a way to check whether parsing is needed or not. Or maybe there's a function out there that can extract the parsed value without altering the original JSON? I know, returning a new variable from the function would be the easy solution:
.data(function (d){
var a = d.Data.slice(d.Data.length-1,d.Data.length);
a.forEach(function (k){k["Id"]=d.Id; k["Date"]=parseDate(d.Date);});
return a;
})
Changing date format in the JSON could work, but where's the fun in taking the simple route?
I'm a JavaScript newbie and don't have time for theory :P But I'm hoping someone out there has a solid answer for me.
Pardon my English and disregard the theory chatter, thanks for your attention :)