arrayOfProfileStats = [{
defaults: {
sessionID: '1',
uID: '',
keySig: '',
tempo: '',
time: '00:10:00',
datetime: '01/01/2000'
}
},
{
defaults: {
sessionID: '2',
uID: '',
keySig: '',
tempo: '',
time: '00:10:00',
datetime: '01/01/2000'
}
},
{
defaults: {
sessionID: '3',
uID: '',
keySig: '',
tempo: '',
time: '00:10:00',
datetime: '01/02/2000'
}
}
];
function msToHMS(ms) {
// 1- Convert to seconds:
var seconds = ms/1000;
// 2- Extract hours:
var hours = parseInt(seconds/3600); // 3,600 seconds in 1 hour
seconds = seconds % 3600; // seconds remaining after extracting hours
// 3- Extract minutes:
var minutes = parseInt(seconds/60); // 60 seconds in 1 minute
// 4- Keep only seconds not extracted to minutes:
seconds = seconds % 60;
return (hours + ":" + minutes + ":" + seconds);
}
function toMilliseconds(time) {
var a = time.split(':'); // split it at the colons
return ((parseInt(a[0]) * 60 * 60 + parseInt(a[1]) * 60 + parseInt(a[2])) * 1000);
}
var timeForEachDay = function() {
var grouped = (_.groupBy(arrayOfProfileStats, function(stat) {
return stat.defaults.datetime;
}));
return _.map(grouped, function(profileDate, key) {
// At this point we should have a new object that groups the profileStats by datetime
// Now we need to sum the times using reduce()
return {
[key]: msToHMS(_.reduce(_.map(profileDate, function(date) {
return toMilliseconds(date.defaults.time);
}), function(total, time) {
return total + time;
}, 0))
};
});
}
console.log(timeForEachDay());
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
이러한 속성의 값은 무엇입니까? –
어떤 종류의 datetime 형식입니까? 순간을 사용하고 있습니까? –