2017-04-26 19 views
0

我有一個集合,其中的模型如下所示:有一個屬性,和結合骨幹機型值

ProfileStat = Backbone.Model.extend({ 
defaults: { 
    sessionID: '', 
    uID: '', 
    keySig: '', 
    tempo: '', 
    time: '', 
    datetime: '' 
}, 
url: function() { 
    return apiUrl('Sessions'); 
} 
}); 

我想遍歷這個集合中的所有模型,組合所有車型相同的日期時間,並加起所有的時間值。

給你一些情況下,我們基本上都被投入會議在我們的網站用戶,每個會話被記錄了他們發揮的時間量,但通常他們一天大約10次推杆英寸我們希望將任何一天的所有這些會話結合起來,並將他們玩過的總時間輸出到圖表中。

誰能幫我一起根據當天用戶已發揮這些模型相結合的方法,並添加了他們打了那一天所有的時間?謝謝!

+0

這些屬性的值是什麼? –

+0

什麼樣的日期時間格式?你在使用momentjs嗎? –

回答

0

我結合了幾個不同的下劃線方法的使用要做到這一點。按照日期分組,按地圖創建對象,看看我們想要的方式,然後減少對項目進行求和。讓我知道,如果這一切是有道理的

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>

+0

這有什麼好運?不知道是否你有機會看到這一點,但我得到了它的工作時間使用的格式,如「00:10:05」 – Chris

+0

對不起,我解決了這個問題有幾個突出的功能。我會發布他們 – user2796352

0

我用一對夫婦強調的方法來解決這個問題。第一個函數過濾集合中的所有模型,以確保被抓取的模型在特定日期之後。第二個函數對所有這些選擇的模型進行排序,並總結播放的時間。

filtered = this.filter(function (ProfileStat) { 
     //convert the current models datetime to unix 
     unixDateTime = Moment(ProfileStat.get("datetime")).valueOf(); 
     //convert the interval date to unix 
     intervalDate = Moment(intervalDate).valueOf(); 
     //only return the sessions that are ahead of the interval date 
     return unixDateTime >= intervalDate; 

     //iterate through each model returned from the filtered function and combine all the sessions for any given date 
    }).forEach(function(ProfileStat){ 
     //format the current model date time to MM/DD/YYYY 
     ProfileStat.attributes.datetime = Moment(ProfileStat.get('datetime')).format('MM/DD/YYYY'); 
     //store this datetime in a variable 
     var date = ProfileStat.attributes.datetime; 
     if (dateArray.indexOf(date) == -1) { 
      dateArray.push(date); 
      var obj = {datetime: date, timePlayed: ProfileStat.attributes.timePlayed,}; 
      resultArray.push(obj); 
     } 
     else { 
      resultArray[dateArray.indexOf(date)].timePlayed += ProfileStat.attributes.timePlayed; 
     } 
    });