[英]How to count a boolean value from a map
使用的框架是 Angular 8
我有一個名為teams
的數組,我可以通過使用teams.length
來獲取其長度。 現在,對於每個計數, teams
都有一個名為teamInfo
的 map。 這個 teamInfo map 再次有一個 boolean 值isClosed
可以通過teams.teamInfo.isClosed
為每個計數訪問,當它 json 到 ngforloop 時,這是
*ngFor=" let teamID of JSObject.keys(teams); trackBy: trackByFn
<app-team-card
[team]="teams[teamID]"
[isClosed]="teams[teamID].teamInfo.isClosed">
</app-team-card>
我的目標是獲取所有具有isClosed =true.
我的嘗試是
getClosedTeams(teams) {
let closedTeams = teams.teamInfo.filter((isClosed) => {
return isClosed.select == true;
});
console.log(closedTeams.length);
}
您可以使用.reduce執行以下操作:
const team01 = new Map(); team01.set('name', 'team-01'); team01.set('isClosed', false); const team02 = new Map(); team02.set('name', 'team-02'); team02.set('isClosed', true); const team03 = new Map(); team03.set('name', 'team-01'); team03.set('isClosed', true); const teams = [team01, team02, team03]; function getClosedTeamsCount (teams) { return teams.reduce((acc, teamInfo) => { return Number(teamInfo.get('isClosed')) + acc; }, 0) } console.log(getClosedTeamsCount(teams));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.