gpt4 book ai didi

javascript - Angular 2合并相同的变量号

转载 作者:行者123 更新时间:2023-11-30 19:58:22 25 4
gpt4 key购买 nike

我像这样从数据库中获取数字:

    getTeamPoints(team) {
console.log(team);
let a = (<any>Object).values(team.tasks).reduce((total, entry) => total + entry.pt, 0);
console.log('a = ',a)
}

对于每个 number 我得到 value(仅示例不是真实值):

a = 10;
a = 15;
a = 20;
a = 5;
a = 0;

如何将那些a合并为一个数字,即- 10+15+20+5+ 0 = 50 ?

编辑:console.log(团队):

{"members":{"-LT8U_zn5T9HuSlh03lw":{"name":"Andrius"}},"name":"Frontas","tasks":{"-LT8UbPIJMrR52P4hvHJ":{"name":"Jobas","pt":5},"-LT8UcOAvqPk_o3fUl7y":{"name":"Jobas2","pt":5}}}

{"members":{"-LT8UenQm-WzElOQhIrX":{"name":"Tomas"}},"name":"Backas","tasks":{"-LT8UfmIezqiMDv3WCpA":{"name":"Jobas3","pt":5}}}

{"members":{"-LT986cZrN6Q1sqIjMZF":{"name":"AAA"}},"name":"Tetas","tasks":{"-LT988EvCySLOutd5AVK":{"done":true,"name":"Jobas","pt":15},"-LT989cIEqr4ObqNyfiA":{"done":false,"name":"XX","pt":16}}}

编辑:遍历团队:

<mat-card-content *ngFor="let team of objectValues(project[2])>
<h2 class="right-text">{{getTeamPoints(team)}}%</h2>
</mat-card-content>

编辑:objectValues 函数:

  objectValues(obj) { 
if(obj){
return Object.values(obj);
}

控制台日志(团队):

["teams",{"members":{"-LT8U_zn5T9HuSlh03lw":{"name":"Andrius"}},"name":"Frontas","tasks":{"-LT8UbPIJMrR52P4hvHJ":{"name":"Jobas","pt":5},"-LT8UcOAvqPk_o3fUl7y":{"name":"Jobas2","pt":5}}},{"members":{"-LT8UenQm-WzElOQhIrX":{"name":"Tomas"}},"name":"Backas","tasks":{"-LT8UfmIezqiMDv3WCpA":{"name":"Jobas3","pt":5}}},{"members":{"-LT986cZrN6Q1sqIjMZF":{"name":"AAA"}},"name":"Tetas","tasks":{"-LT988EvCySLOutd5AVK":{"done":true,"name":"Jobas","pt":15},"-LT989cIEqr4ObqNyfiA":{"done":false,"name":"XX","pt":16}}}]

最佳答案

像这样更改您的组件:

<mat-card-content>
<h2 class="right-text">
{{ getProjectTeamsTotalPoints(objectValues(project[2])) }}
</h2>
</mat-card-content>

然后在你的类中添加这个方法:

getProjectTeamsTotalPoints(teams): number {
return teams.reduce((total, team) => {
if (!team || !team.tasks) {
return total;
}
return Object.values(team.tasks).reduce((pts, task) => {
return pts + task.pt;
}, total);
}, 0);
}

同时更新您的 objectValues 函数以始终返回一个数组:

objectValues(obj) {
return Object.values(obj || {});
}

基本上,您减少团队并为每个团队减少他们的分数,这样您就可以获得项目的总摘要。

关于javascript - Angular 2合并相同的变量号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53681926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com