我有一个低于
JSON:
[{"brand":"abc"},{"brand":"xyz"},{"brand":"abc"},{"brand":"por"},{"brand":"xyz"}]
使用ng-repeat,我如何显示如下 –
Brand Occurances abc (3) xyz (2) por (1)
解决方法
您可以创建一个自定义函数,该函数将使用repeatvie值从现有数组返回计数(出现)
与过滤器一起显示JSON中的唯一值:
$scope.getCount = function(i) { var iCount = iCount || 0; for (var j = 0; j < $scope.brands.length; j++) { if ($scope.brands[j].brand == i) { iCount++; } } return iCount; }
AND过滤器将如下所示:
app.filter('unique',function() { return function (arr,field) { var o = {},i,l = arr.length,r = []; for(i=0; i<l;i+=1) { o[arr[i][field]] = arr[i]; } for(i in o) { r.push(o[i]); } return r; }; })