【发布时间】:2019-01-21 05:38:47
【问题描述】:
您好,我有数组,我需要执行各种操作,例如求和、总计、平均值。所有这三个都实现了,现在我需要在数组中找到最小值和最大值。我被困在下面的是代码。
下面是TS部分
people: Array<number> = [1, 2, 3, 4, 5];
total: number = 0;
arrayLength: number = this.people.length;
average: number = 0;
sum() {
for (var i in this.people) { this.total += this.people[i]; }
}
ngOnInit() {
this.sum();
this.average = (this.total / this.arrayLength);
}
下面是 HTML 部分
<span *ngFor="let p of people" style="font-size:18px">{{p}} </span><br><br>
<button >Quantity</button> = {{arrayLength}}<Br><br>
<button >Average</button> = {{average}}<Br><br>
<button >Sum</button> <span *ngIf="sumShow"> = {{total}}</span><Br><br>
【问题讨论】:
标签: arrays angular typescript angular6