gpt4 book ai didi

javascript - angular.js Controller 中的过滤和计数数组

转载 作者:行者123 更新时间:2023-11-30 16:42:18 26 4
gpt4 key购买 nike

我的 Angular 应用程序中有一个对象数组,每个对象都有一个“就绪”字段,它是一个时间戳。我想统计就绪时间戳早于当前时间的对象数量。我该怎么做?

我有:

$scope.getDatetime = new Date();
$scope.numberReady = $filter('filter')($scope.array, {ready < $scope.getDatetime}).length;

显然我不能使用 ready < $scope.getDatetime ,但从逻辑上讲,这就是我想做的。

最佳答案

您可以使用纯 ES5 过滤器方法,这里不需要 Angular:

$scope.getDatetime = new Date();
$scope.numberReady = $scope.array.filter(function(obj) {
return obj.ready < $scope.getDatetime;
}).length;

...虽然你也可以在这里使用 Angular 过滤器:

$scope.getDatetime = new Date();
$scope.numberReady = $filter('filter')($scope.array, function(obj) {
return obj.ready < $scope.getDatetime;
}).length;

但由于它只是原生事物的包装器,因此在这种情况下并不理想。

关于javascript - angular.js Controller 中的过滤和计数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com