这是我的json数据
$scope.players = [
{name: 'Bruce Wayne', shirt: 'XXL', pants: '42', shoes: '12'},
{name: 'Wayne Gretzky', shirt: 'XL', pants: '38', shoes: '10'},
{name: 'Michael Jordan', shirt: 'M', pants: '32', shoes: '9'},
{name: 'Rodman', shirt: 'XSXL', pants: '42', shoes: '11'},
{name: 'Jake Smitz', shirt: 'XXL', pants: '42', shoes: '12'},
{name: 'Will Will', shirt: 'XXLL', pants: '42', shoes: '12'},
{name: 'Youasdf Oukls', shirt: 'XL', pants: '38', shoes: '10'},
{name: 'Sam Sneed', shirt: 'XL', pants: '38', shoes: '10'},
{name: 'Bill Waxy', shirt: 'M', pants: '32', shoes: '9'},
{name: 'Javier Xavior', shirt: 'M', pants: '32', shoes: '9'},
{name: 'Bill Knight', shirt: 'M', pants: '32', shoes: '9'},
{name: 'One More', shirt: 'M', pants: '100', shoes: '9'},
{name: 'Player One', shirt: 'XXL', pants: '42', shoes: '100'},
{name: 'Space Cadet', shirt: 'XXL', pants: '42', shoes: '100'},
{name: 'Player Two', shirt: 'XXXXL', pants: '42', shoes: '12'}
];
这是我的HTML代码,用于计算对象数量
<div>
<div ng-repeat="shoes in shoesGroup">
<b><input type="checkbox" ng-model="useShoes[shoes]"/>{{shoes}}</b>
<span>({{(filteredPlayers | filter:shoes :true).length}})</span>
</div>
</div>
问题是我想要显示大小为100的鞋子数量,根据我的数据应该给出2的答案,但它正在读取我的JSON数据中的所有100个并给出答案为3因为那里有一百多个裤子:100,
我的回答应该是
pants 100(1)
shoes 100(2)
但是我得到裤子100(3) shoes100(3) 请帮忙解决这个问题
这是鞋组的控制器,只显示唯一的项目
$scope.shoesGroup = uniqueItems(filterAfterShirts, 'shoes');
var filterAfterShoes = [];
selected = false;
for (var j in filterAfterShirts) {
var p = filterAfterShirts[j];
for (var i in $scope.useShoes) {
if ($scope.useShoes[i]) {
selected = true;
if (i == p.shoes) {
filterAfterShoes.push(p);
break;
}
}
}
}
if (!selected) {
filterAfterShoes = filterAfterShirts;
}
$scope.filteredPlayers = filterAfterShoes;
}, true);
这是唯一项目的功能
var uniqueItems = function (data, key) {
//alert("test");
var result = new Array();
for (var i = 0; i < data.length; i++) {
var value = data[i][key];
if (result.indexOf(value) == -1) {
result.push(value);
}
}
return result;
};
答案 0 :(得分:0)
如果您只想显示尺码为100的鞋子和裤子的数量,您可以这样做:
$scope.p = 0, $scope.s = 0;
for(var i=0; i<$scope.players.length; i++) {
if($scope.players[i].pants == '100') {
$scope.p++;
}
if($scope.players[i].shoes == '100') {
$scope.s++;
}
}
在HTML中:
<div>
shoes({{s}})
pants({{p}})
</div>