我有以下数据:
$scope.icons = [{
"id": 1,
"bonuses": [
{ "id": 1, "name": "Rob", "amount": 10, "foreman": 1, "percA": 1, "percB": 0 },
{ "id": 2, "name": "Mark", "amount": 20, "foreman": 1, "percA": 1, "percB": 0 },
]
}, {
"id": 2,
"bonuses": [
{ "id": 1, "name": "Rob", "amount": 11, "foreman": 1, "percA": 1, "percB": 0 },
{ "id": 2, "name": "", "amount": 30, "foreman": 1, "percA": 1, "percB": 0 },
]
}, {
"id": 3,
"bonuses": [
{ "id": 1, "name": "Rob", "amount": 10, "foreman": 1, "percA": 1, "percB": 0 },
{ "id": 2, "name": "Kent", "amount": 35, "foreman": 1, "percA": 1, "percB": 0 },
]
}];
控制器内部的这个功能:
$scope.getBonusPeople = function() {
var bonusData = [];
angular.forEach($scope.icons, function(item) {
angular.forEach(item.bonuses, function(bonusLine) {
if (bonusData.indexOf(bonusLine) == -1 ) {
bonusData.push(bonusLine);
}
})
});
return $scope.bonusPeople = bonusData;
}
这就是结果:
Rob - 10
Mark - 20
Rob - 11
- 30
Rob - 10
Kent - 35
我试图找到bonuses.name有值的行。
结果应该是
Rob - 10
Mark - 20
Rob - 11
Rob - 10
Kent - 35
我该怎么做。
答案 0 :(得分:2)
您需要添加一项检查以查看name是否为空字符串。请参阅下面的代码段。
╔═════╦════════╦══════════════╗
║ ID ║ Desc1 ║ BALANCE_DATE ║
╠═════╬════════╬══════════════╣
║ ID1 ║ Desc 3 ║ 2015-10-22 ║
║ ID2 ║ Desc 3 ║ 2015-09-28 ║
║ ID3 ║ Desc 1 ║ 2015-10-18 ║
╚═════╩════════╩══════════════╝
答案 1 :(得分:0)
检查名称是否有值。如果是,请继续执行当前逻辑:
angular.forEach($scope.icons, function(item) {
angular.forEach(item.bonuses, function(bonusLine) {
if(bonusLine.name!=='') {
if (bonusData.indexOf(bonusLine) == -1 ) {
bonusData.push(bonusLine);
}
}
})
});