Javascript 选择进行部分和的筛选值
有两个班:教授和学生:Javascript 选择进行部分和的筛选值,javascript,html,arrays,angular,typescript,Javascript,Html,Arrays,Angular,Typescript,有两个班:教授和学生: 教授ts student.ts 为了将每个学生相对于老师的分数相加,我做了: sel: number[] = [] prof: Professor[] stud: Student[] totalScore: number partial: number var: boolean = false total() { const total = this.stud .filter(c => this.sel.map(m => +m).includ
- 教授ts
- student.ts
sel: number[] = []
prof: Professor[]
stud: Student[]
totalScore: number
partial: number
var: boolean = false
total() {
const total = this.stud
.filter(c => this.sel.map(m => +m).includes(c.id))
.reduce((a, c) => a + c.score, 0);
this.totalScore = total
this.var = true
}
这是页面的HTML:
{{p.name}}
{{s.name}}|
{{s.score}}
去
总计:{{totalScore}}
将总方法更改为:-
total() {
const total = this.stud
.filter(c => this.sel.map(m => +m).includes(c.id))
.slice(0,3)
.reduce((a, c) => a + c.score, 0);
this.totalScore = total
this.variable = true
}
工作stackblitz:-你能提供stackblitz吗?@AakashGarg是的,这是你期望的输出吗?例如,前三个选择的部分总和(丹尼尔、卢克、马克)=6+8+3=17,而不计算后续选择的总和
sel: number[] = []
prof: Professor[]
stud: Student[]
totalScore: number
partial: number
var: boolean = false
total() {
const total = this.stud
.filter(c => this.sel.map(m => +m).includes(c.id))
.reduce((a, c) => a + c.score, 0);
this.totalScore = total
this.var = true
}
total() {
const total = this.stud
.filter(c => this.sel.map(m => +m).includes(c.id))
.slice(0,3)
.reduce((a, c) => a + c.score, 0);
this.totalScore = total
this.variable = true
}