I'm playing with a data set that counts the number of commits a person makes at a certain hour. I can create an array objects for the hour a person made a commit and how many times they committed:
[ { hour: '6', commits: 2 },
{ hour: '7', commits: 6 },
{ hour: '8', commits: 6 },
{ hour: '9', commits: 4 },
{ hour: '10', commits: 4 },
{ hour: '11', commits: 6 },
{ hour: '12', commits: 18 },
{ hour: '13', commits: 18 },
{ hour: '14', commits: 14 },
{ hour: '15', commits: 30 },
{ hour: '16', commits: 24 },
{ hour: '17', commits: 18 },
{ hour: '18', commits: 24 },
{ hour: '19', commits: 22 },
{ hour: '20', commits: 6 },
{ hour: '21', commits: 16 },
{ hour: '22', commits: 8 } ]
Where I'm stuck is trying to fill in the missing hours. So in the example above, I would need to include the hours where no commits were made. I'm using a 0-24 range for an entire day. This is what I'm hoping to get as a result:
[
{ hour: '0', commits: 0 },
{ hour: '1', commits: 0 },
{ hour: '2', commits: 0 },
{ hour: '3', commits: 0 },
{ hour: '4', commits: 0 },
{ hour: '5', commits: 0 },
{ hour: '6', commits: 2 },
{ hour: '7', commits: 6 },
{ hour: '8', commits: 6 },
{ hour: '9', commits: 4 },
{ hour: '10', commits: 4 },
{ hour: '11', commits: 6 },
{ hour: '12', commits: 18 },
{ hour: '13', commits: 18 },
{ hour: '14', commits: 14 },
{ hour: '15', commits: 30 },
{ hour: '16', commits: 24 },
{ hour: '17', commits: 18 },
{ hour: '18', commits: 24 },
{ hour: '19', commits: 22 },
{ hour: '20', commits: 6 },
{ hour: '21', commits: 16 },
{ hour: '22', commits: 8 },
{ hour: '23', commits: 0 },
{ hour: '24', commits: 0 }
]
Here is the function I'm using to create these objects:
var getAuthorAndHourCounts = (groupedByAuthor) => {
var result = _.map(groupByAuthor, (value, key, list) => {
var author = key;
var hours = _.countBy(value, 'hour');
var commitHourList = [];
_.each(hours, (v,k) => {
var obj = {};
obj.hour = k;
obj.commits = v;
commitHourList.push(obj);
});
return {
person: author,
times: commitHourList
};
});
return result;
};
How can I add the missing times? I was thinking of creating a list (0-24) and then seeing if an hour is not in the list to add it with a value of zero to the object. Not sure how that can be accomplished.