I have an array of objects which contain an end and a start date. But the period between dates may straddle midnight. If they do I wish to replace the object with two objects, the first with the previous start date and an end date of midnight. The second with a start time of 00:00 the next day and end date of what it was previously.
So for example if the array contains an object such as this,
{
"start": "2016-11-04 22:00",
"end": "2016-11-05 03:00"
}
I want to replace that object with two objects,
{
"start": "2016-11-04 22:00",
"end": "2016-11-04 24:00"
},
{
"start": "2016-11-05 00:00",
"end": "2016-11-05 03:00"
}
Here is my attempt using data.push(). Clearly this is not the way to do it:
d3.json("data.json", function(data) {
var parseTime = d3.timeParse("%Y-%m-%d %H:%M");
data.forEach(function(d) {
d.commence = parseTime(d.start);
d.conclude = parseTime(d.end);
if (d.commence.getDay() != d.conclude.getDay()) {
midnight = d.commence.getFullYear() + "-" + d.commence.getMonth() + "-" + d.commence.getDay() + " 24:00";
morning = d.conclude.getFullYear() + "-" + d.conclude.getMonth() + "-" + d.conclude.getDay() + " 00:00";
data.push({
"start": d.start,
"end": midnight
}, {
"start": morning,
"end": d.end
})
}
});
...
So how can I add or remove an object as I am iterating through the array of objects?
The data starts like this,
[
{
"start": "2016-11-01 12:00",
"end": "2016-11-01 22:00"
},
{
"start": "2016-11-02 02:00",
"end": "2016-11-02 18:00"
},
{
"start": "2016-11-03 09:00",
"end": "2016-11-03 12:00"
},
{
"start": "2016-11-04 22:00",
"end": "2016-11-05 03:00"
},
{
"start": "2016-11-06 12:00",
"end": "2016-11-06 23:00"
}
]
And in the console I can see it ultimately looks like this,
[
{
"start": "2016-11-01 12:00",
"end": "2016-11-01 22:00",
"commence": "2016-11-01T12:00:00.000Z",
"conclude": "2016-11-01T22:00:00.000Z"
},
{
"start": "2016-11-02 02:00",
"end": "2016-11-02 18:00",
"commence": "2016-11-02T02:00:00.000Z",
"conclude": "2016-11-02T18:00:00.000Z"
},
{
"start": "2016-11-03 09:00",
"end": "2016-11-03 12:00",
"commence": "2016-11-03T09:00:00.000Z",
"conclude": "2016-11-03T12:00:00.000Z"
},
{
"start": "2016-11-04 22:00",
"end": "2016-11-05 03:00",
"commence": "2016-11-04T22:00:00.000Z",
"conclude": "2016-11-05T03:00:00.000Z"
},
{
"start": "2016-11-06 12:00",
"end": "2016-11-06 23:00",
"commence": "2016-11-06T12:00:00.000Z",
"conclude": "2016-11-06T23:00:00.000Z"
},
{
"start": "2016-11-04 22:00",
"end": "2016-10-5 24:00"
},
{
"start": "2016-10-6 00:00",
"end": "2016-11-05 03:00"
}
]
The full code is on github and a demo is running on gh-pages.
Any suggestions would be greatly appreciated,
Thanks