Javascript 通过一个属性和总和值组合主干模型

Javascript 通过一个属性和总和值组合主干模型,javascript,backbone.js,underscore.js,Javascript,Backbone.js,Underscore.js,我有一个集合,其中的模型如下所示: ProfileStat = Backbone.Model.extend({ defaults: { sessionID: '', uID: '', keySig: '', tempo: '', time: '', datetime: '' }, url: function() { return apiUrl('Sessions'); } }); 我想遍历这个集合中的所有模型,组合所有具有相同dateti

我有一个集合,其中的模型如下所示:

ProfileStat = Backbone.Model.extend({
defaults: {
    sessionID: '',
    uID: '',
    keySig: '',
    tempo: '',
    time: '',
    datetime: ''
},
url: function() {
    return apiUrl('Sessions');
}
});
我想遍历这个集合中的所有模型,组合所有具有相同datetime的模型,并将它们的所有时间值相加

为了给你一些背景,我们基本上有用户在我们的网站上进行会话,每个会话都会记录下他们玩的时间,但通常他们每天大约会进行10次会话。我们希望将任意一天的所有这些会话合并,并将它们播放的总时间输出到一个图表中


有没有人能帮我找到一种方法,根据用户玩过的那一天来组合这些模型,并把他们玩过的那一天的时间加起来?谢谢大家!

为此,我结合使用了几种不同的下划线方法。Group by to Group by date(按日期分组),映射以创建与我们所需外观完全一致的对象,并减少以求和项目。让我知道这一切是否有意义

arrayOfProfileStats=[{
默认值:{
会话ID:'1',
uID:“”,
键值:“”,
节奏:'',
时间:“00:10:00”,
日期时间:'01/01/2000'
}
},
{
默认值:{
会话ID:'2',
uID:“”,
键值:“”,
节奏:'',
时间:“00:10:00”,
日期时间:'01/01/2000'
}
},
{
默认值:{
sessionID:'3',
uID:“”,
键值:“”,
节奏:'',
时间:“00:10:00”,
日期时间:2000年2月1日
}
}
];
功能msToHMS(ms){
//1-转换为秒:
var秒=毫秒/1000;
//2-提取时间:
var hours=parseInt(秒/3600);//1小时内3600秒
秒=秒%3600;//提取小时后剩余的秒数
//3-摘录分钟:
var minutes=parseInt(秒/60);//1分钟内60秒
//4-仅保留未提取到分钟的秒数:
秒=秒%60;
返回时间(小时+“:“+分钟+”:“+秒);
}
功能时间(秒){
var a=time.split(“:”);//在冒号处拆分
返回((parseInt(a[0])*60*60+parseInt(a[1])*60+parseInt(a[2]))*1000);
}
var timeForEachDay=函数(){
var grouped=(u.groupBy(arrayOfProfileStats,函数(stat)){
返回stat.defaults.datetime;
}));
返回映射(分组,函数(profileDate,键){
//此时,我们应该有一个新对象,它按日期时间对profileStats进行分组
//现在我们需要使用reduce()对时间求和
返回{
[键]:msToHMS(u.reduce(u.map)profileDate,function(date){
返回时间秒(date.defaults.time);
}),函数(总计,时间){
返回总数+时间;
}, 0))
};
});
}
log(timeForEachDay())

我使用了几个下划线方法来解决这个问题。第一个函数过滤集合中的所有模型,以确保抓取的模型在某个日期之后。第二个函数对所有选择的模型进行排序,并总结播放的时间

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;
         }
    });

这些属性的值是什么?什么类型的日期时间格式?你在用momentjs吗?运气好吗?不确定您是否有机会看到这个,但我使用“00:10:05”之类的时间格式使它工作。对不起,我用一些下划线函数解决了这个问题。我会把它们寄出去