在 Array Angular 和 Typescript 中查找最小和最大元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51840141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:35:03  来源:igfitidea点击:

Find smallest & largest element in Array Angular & Typescript

arraysangulartypescriptangular6

提问by Jignesh Mistry

Hello All I have array & i need to perform various operation like sum, total, average. All these 3 are achieved, Now I need to find the minimum & maximum value in array. I am stucked on this below is the code.

大家好,我有数组,我需要执行各种操作,例如总和、总计、平均值。所有这三个都实现了,现在我需要找到数组中的最小值和最大值。我被困在下面是代码。

Below is TS Part

以下是 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);
  }

Below is HTML Part

下面是 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>

回答by Tomasz Kula

Use Math.maxand Math.mincombined with the spread operator.

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

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

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

回答by Krishna Rathore

use reducefor this.

reduce为此使用。

Demo on Stackblitz

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>

回答by baao

You could create yourself a little helper class that does those operations for you and is reusable throughout your code

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

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());

Note:

笔记:

Depending on use case, you'll want to extend this to cache results...

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

回答by Anuradha Gunasekara

You can use Array.reduceand Math.max(), Math.min()for this.

为此,您可以使用Array.reduce和。Math.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

And you can find a working example in here

你可以在这里找到一个工作示例