【问题标题】:Find smallest & largest element in Array Angular & Typescript在 Array Angular 和 Typescript 中查找最小和最大元素
【发布时间】: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


    【解决方案1】:

    Math.maxMath.min 与展开运算符结合使用。

    get max() {
      return Math.max(...this.people);
    }
    
    get min() {
      return Math.min(...this.people);
    }
    

    【讨论】:

      【解决方案2】:

      为此使用reduce

      Stackblitz

      上的演示
        sum() {
          this.total = this.people.reduce((a, b)=>a + b); 
        }
      
        ngOnInit() {
          this.sum();
          this.max = this.people.reduce((a, b)=>Math.max(a, b)); 
          this.min = this.people.reduce((a, b)=>Math.min(a, b)); 
          this.average = (this.total / this.arrayLength);
        }
      
      
      <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 > = {{total}}</span><Br><br>
      
      <button >Max</button> <span > = {{max}}</span><Br><br>
      <button >Min</button> <span > = {{min}}</span><Br><br>
      

      【讨论】:

      • 兄弟,我需要显示两个最小值,在我的情况下为 1,最大值为 5
      • 我们可以在同一个数组上计算标准差吗?
      • 哦,那会很有帮助。如果我们使用相同的数组标准偏差值将是 1.414214
      • 好的兄弟慢慢来。完成后告诉我
      • 嗨@JigneshMistry 我有更新链接Standard Deviation 用于计算平均值。
      【解决方案3】:

      您可以为此使用 Array.reduceMath.max()Math.min()

      const people = [1,2,3,4,5];
      
      const max = people.reduce((a, b) => Math.max(a, b));  // 5
      
      const min = people.reduce((a, b) => Math.min(a, b));  // 1
      
      const sum = people.reduce((a, b) => a+b, 0);  // 15
      

      您可以在 here 中找到一个工作示例

      【讨论】:

      • 谢谢你的朋友:)
      【解决方案4】:

      您可以为自己创建一个小助手类,为您执行这些操作并且可在整个代码中重用

      export class MathOps {
        array: number[];
      
        constructor(array: number[]) {
          this.array = array;
        }
      
        sum(): number {
          return this.array.reduce((a, b) => a + b, 0);
        }
      
        avg(): number {
          return this.sum() / this.array.length;
        }
      
        max(): number {
          return Math.max(...this.array);
        }
      
        min(): number {
          return Math.min(...this.array);
        }
      }
      
      const ops = new MathOps([1, 2, 3, 4, 5]);
      console.log(ops.avg());
      console.log(ops.max());
      console.log(ops.min());
      console.log(ops.sum());
      

      注意:

      根据用例,您需要将其扩展到缓存结果...

      【讨论】:

      • 解决了我所有问题的好帮手
      猜你喜欢
      • 2013-12-15
      • 2020-05-08
      • 2021-12-25
      • 1970-01-01
      • 2018-10-17
      • 2021-05-31
      • 2016-03-18
      • 2020-11-01
      • 1970-01-01
      相关资源
      最近更新 更多