Skip to content

Instantly share code, notes, and snippets.

@znbailey
Forked from rjurney/input.json
Created July 13, 2012 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save znbailey/3102624 to your computer and use it in GitHub Desktop.
Save znbailey/3102624 to your computer and use it in GitHub Desktop.
Fill in blanks in a time series in Javascript where the range of the data is "00"-"23"
[{"total":16,"sent_hour":"08"},{"total":16,"sent_hour":"09"},{"total":24,"sent_hour":"10"},{"total":14,"sent_hour":"11"},{"total":6,"sent_hour":"12"},{"total":22,"sent_hour":"13"},{"total":32,"sent_hour":"14"},{"total":14,"sent_hour":"15"},{"total":10,"sent_hour":"16"},{"total":10,"sent_hour":"17"},{"total":4,"sent_hour":"18"},{"total":8,"sent_hour":"20"},{"total":6,"sent_hour":"21"},{"total":20,"sent_hour":"22"},{"total":2,"sent_hour":"23"}]
[{"sent_hour":"00","total":0},{"sent_hour":"01","total":0},{"sent_hour":"02","total":0},{"sent_hour":"03","total":0},{"sent_hour":"04","total":0},{"sent_hour":"05","total":0},{"sent_hour":"06","total":0},{"sent_hour":"07","total":0},{"total":16,"sent_hour":"08"},{"total":16,"sent_hour":"09"},{"total":24,"sent_hour":"10"},{"total":14,"sent_hour":"11"},{"total":6,"sent_hour":"12"},{"total":22,"sent_hour":"13"},{"total":32,"sent_hour":"14"},{"total":14,"sent_hour":"15"},{"total":10,"sent_hour":"16"},{"total":10,"sent_hour":"17"},{"total":4,"sent_hour":"18"},{"sent_hour":"19","total":0},{"total":8,"sent_hour":"20"},{"total":6,"sent_hour":"21"},{"total":20,"sent_hour":"22"},{"total":2,"sent_hour":"23"}]
function makeHourRange(num) {
return num < 10 ? "0" + num : num;
}
function fillBlanks(ourHours, rawData) {
var ourData = [];
var lastFoundPos = 0;
for (var i = 0, n = ourHours.length; i < n; i++) {
var curHour = ourHours[i];
if (rawData[lastFoundPos]['sent_hour'] === curHour) {
ourData.push(rawData[lastFoundPos]);
lastFoundPos++;
} else {
ourData.push({'total': 0, 'sent_hour': curHour});
}
}
return ourData;
}
var rawData = [{"total":16,"sent_hour":"08"},{"total":16,"sent_hour":"09"},{"total":24,"sent_hour":"10"},{"total":14,"sent_hour":"11"},{"total":6,"sent_hour":"12"},{"total":22,"sent_hour":"13"},{"total":32,"sent_hour":"14"},{"total":14,"sent_hour":"15"},{"total":10,"sent_hour":"16"},{"total":10,"sent_hour":"17"},{"total":4,"sent_hour":"18"},{"total":8,"sent_hour":"20"},{"total":6,"sent_hour":"21"},{"total":20,"sent_hour":"22"},{"total":2,"sent_hour":"23"}];
var hourRange = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"];
var filledData = fillBlanks(hourRange, rawData);
console.log(JSON.stringify(filledData));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment