我正在跟踪用户点击应用中的社交按钮的次数。我想每天只给他们一次点击。我目前通过社交平台过滤点击次数,但我无法弄清楚如何按天分组,每天只计算1次。以下是我目前的代码:
控制器:
Data.get('stats/'+$scope.uid).then(function(data){
$scope.stats = data.data;
// determine if the user has any stats
$scope.hasStats = $scope.stats.length;
$scope.topUsersObjects = $filter('filter')($scope.stats, {
action: "completed"
});
$scope.facebookObjects = $filter('filter')($scope.stats, {
platform: "facebook",
action: "completed"
});
$scope.twitterObjects = $filter('filter')($scope.stats, {
platform: "twitter",
action: "completed"
});
$scope.facebookClickedObjects = $filter('filter')($scope.stats, {
platform: "facebook",
action: "clicked"
});
$scope.twitterClickedObjects = $filter('filter')($scope.stats, {
platform: "twitter",
action: "clicked"
});
$scope.labels = ["Facebook", "Twitter", "Clicked"];
var totalClicked = $scope.facebookClickedObjects.length + $scope.twitterClickedObjects.length;
$scope.data = [$scope.facebookObjects.length, $scope.twitterClickedObjects.length, totalClicked];
});
UPDATE(示例$ scope.stats):
Object
action:"clicked ls"
guid:"1200041"
id:"52"
platform:"facebook"
timeclicked:"2015-10-12 21:46:26"
uid:"44043077e53d"
非常感谢任何帮助。
最终更新的答案:
谢谢你@William-b你的回答是有效的,我最后这样做了。如果这不是一个好方法,请告诉我。谢谢大家的帮助。
// facebook group by days, completed
$scope.facebookGroupByDay = _.groupBy($scope.facebookObjects, function(item) {
return item.timeclicked.substring(0,10);
});
// get the number of days with a completed post
$scope.fbCount = Object.keys($scope.facebookGroupByDay);
// twitter group by days, clicked
$scope.twitterGroupByDay = _.groupBy($scope.twitterClickedObjects, function(item) {
return item.timeclicked.substring(0,10);
});
// get the number of days with a clicked tweet
$scope.twCount = Object.keys($scope.twitterGroupByDay);
答案 0 :(得分:1)
听起来您希望reduce
点击一系列点击次数。
var days = {}; // let's keep track of days clicked here during the reduction
var clicksByDay = $scope.stats.reduce(function (clickCount, stat) {
var day = stat.timeclicked.split(' ')[0];
if (!days[day]) {
// we didnt count this day yet
clickCount++;
days[day] = true; // mark this day as counted
}
return clickCount;
}, 0);
如果$ scope.stats看起来像这样:
[
{ timeclicked: '2015-10-12 12:34:56' },
{ timeclicked: '2015-10-12 21:46:26'},
{ timeclicked: '2015-10-13 21:46:26' }
]
然后clicksByDay
在这种情况下为2。
P.S。如果在多个控制器的模板中需要它,那么就没有什么能阻止你为这种类型的减少定义自己的角度过滤器。