我有一个对象数组(500个对象),并希望GroupBy每组50个游戏(10组)。
现在数据是:
var data = [
{ name : "Starburst",
id : 2614
},
{ name : "Pipono",
id : 4587
},
{ name : "New Bail",
id : 7827
}
...
]
HTML
<div ng-repeat="(key, value) in data| groupBy: 'id'"> ... </div>
现在我按ID分组,无法找到解决方法。
我知道有一种方法可以用&#34; for&#34;循环(为每个第50个元素0-50,51-100 ...添加值,并且groupBy该值),但它不是最好和最快的方式。
我怎么能意识到这一点? 提前谢谢。
答案 0 :(得分:0)
您可以使用lodash使用_.sortBy和_.chunk。传递要组合在一起的项目数,
$scope.itemschunk = _.chunk($scope.itemssorted, 50);
<强>样本强>
var app = angular.module('anExample', []);
app.controller('theController', ['$scope',function($scope){
$scope.test = 'this is a test';
$scope.items = [
{ name:"A", id: 30},
{ name:"B", id: 20 },
{ name:"C", id: 200 },
{ name:"D", id: 20 },
{ name:"E", id: 100 },
];
$scope.itemssorted = _.sortBy($scope.items, 'id');
$scope.itemschunk = _.chunk($scope.itemssorted, 2);
}]);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.4.0/lodash.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="anExample" ng-controller="theController">
Hello, {{test}}!
<ul ng-repeat="group in itemschunk">
<li ng-repeat="items in group"> name: {{items.name}}, id: {{items.id}} </li>
</ul>
</div>
&#13;