Jignesh Mistry
Jignesh Mistry

Reputation: 2479

Find smallest & largest element in Array Angular & Typescript

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

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

<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>

Upvotes: 6

Views: 34205

Answers (5)

sweetnandha cse
sweetnandha cse

Reputation: 981

For array of object (Min & Max) :

const arr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}, {a: -1, b: -2, c: -3}];
const min = Math.min(...arr.map(obj => obj.a + obj.b + obj.c));
const max = Math.max(...arr.map(obj => obj.a + obj.b + obj.c));
const minResult = arr.find(obj => obj.a + obj.b + obj.c == min); // {a: -1, b: -2, c: -3}
const maxResult = arr.find(obj => obj.a + obj.b + obj.c == max); // {a: 4, b: 5, c: 6}

[OR]

const arr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}, {a: -1, b: -2, c: -3}];
const min = arr.reduce((p,v) => (p.a + p.b + p.c) < (v.a + v.b + v.c) ? p : v); // {a: -1, b: -2, c: -3}
const max = arr.reduce((p,v) => (p.a + p.b + p.c) > (v.a + v.b + v.c) ? p : v); // {a: 4, b: 5, c: 6}

Upvotes: 0

baao
baao

Reputation: 73241

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...

Upvotes: 4

Krishna Rathore
Krishna Rathore

Reputation: 9687

use reduce for this.

Demo on 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>

Upvotes: 10

Anuradha Gunasekara
Anuradha Gunasekara

Reputation: 6983

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

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

Upvotes: 1

Tomasz Kula
Tomasz Kula

Reputation: 16837

Use Math.max and Math.min combined with the spread operator.

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

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

Upvotes: 10

Related Questions