I have an array of objects where each object has a datetime key/value pair:
var original = [
{
datetime: '2015-07-22 09:00:00'
},
{
datetime: '2015-07-22 11:00:00'
},
{
datetime: '2015-07-23 10:00:00'
}
]
I need to transform them into a new array with objects where the first key/value pair of each object is the date associated with an array of datetimes from the original array of objects. The second key/value pair is an array of the original objects which have a datetime date that matches the new date.
var new = [
{
date: '2015-07-22',
events: [
{
datetime: '2015-07-22 09:00:00'
},
{
datetime: '2015-07-22 11:00:00'
}
]
},
{
date: '2015-07-23',
events: [
{
datetime: '2015-07-23 10:00:00'
}
]
}
];
Below is my current method of making this transformation happen and it is this method that I am seeking to improve upon. It is clunky. It is convoluted. It is hacky.
I am currently selecting the substring from the first object's datetime as currentDate
which only contains the date. Then I'm creating an empty array, new
. as well as an object as datum
which contains a date
key with currentDate
as it's value and an events
key with an empty array as its value.
var currentDate = original[0].datetime.substring(0,10); // => '2015-07-22';
var new = [];
var datum = {
date: currentDate,
events: []
};
Then I'm using a forEach
function on the original
array in the following manner:
original.forEach(function(item){
//if item's date matches currentDate add it to datum object
if (item.datetime.substr(0,10) === currentDate){
datum.events.push(item);
}
//if item's date does not match then it is the next date
else {
//add the previous day's datum object to the new array
new.push(datum);
//set new currentDate and set new datum object
currentDate = item.datetime.substr(0,10);
datum = {
date: currentDate,
events: []
};
//push the current item into the new datum array
datum.events.push(item);
}
});
//push final day's datum to new array
new.push(datum);
What is a more efficient way to transform this data?