-1

Heres my code, which works but i feel like there is a more simple way doing this. The bold part is where i want to change.

interface properties {
    age: number;
    salary: number;
    name: string;
}
let minSalary: any;
let maxSalary: any;

let list: properties[] = [
    {name: "P", age: 12, salary: 100},
    {name: "S", age: 80, salary: 12000},
    {name: "Q", age: 25, salary: 80000},
    {name: "W", age: 55, salary: 45000},
    {name: "E", age: 32, salary: 25000},
    {name: "V", age: 5, salary: 0}
];


**minSalary =  Math.min.apply(Math, list.map( salaries => {
    return salaries.salary;** // This is the code i want to make easier
}
));

console.log(minSalary);
Emissary
  • 9,954
  • 8
  • 54
  • 65
infl8
  • 21
  • 6
  • 1
    Does this answer your question? [Find the min/max element of an Array in JavaScript](https://stackoverflow.com/questions/1669190/find-the-min-max-element-of-an-array-in-javascript) – R. Richards Feb 16 '20 at 17:03

3 Answers3

1

This is a shorter version:

const minSalary =  Math.min(...list.map(x => x.salary))

let list = [
    {name: "P", age: 12, salary: 100},
    {name: "S", age: 80, salary: 12000},
    {name: "Q", age: 25, salary: 80000},
    {name: "W", age: 55, salary: 45000},
    {name: "E", age: 32, salary: 25000},
    {name: "V", age: 5, salary: 0}
];

const minSalary =  Math.min(...list.map(x => x.salary))

console.log(minSalary);

See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • This also needs a test for the list being empty. – QuentinUK Feb 16 '20 at 17:29
  • depends what you expect as output. If you want the minimum price someone has to pay actually `Infinity` is not a bad result for an empty list. – Lux Feb 16 '20 at 17:35
1

You can use reduce

minSalary = list.reduce((least, x) => Math.min(least, x.salary), Infinity)
Emissary
  • 9,954
  • 8
  • 54
  • 65
QuentinUK
  • 2,997
  • 21
  • 20
0

Instead of properties as a interface, make it a class, and have a method called toString and return this.salary (though it is a number type), and then in do Math.min(...list) directly. You can have a method called valueOf instead of toString as well as an alternate, that will work too.

class properties {
  constructor(name, age, salary) {
    this.name = name;
    this.age = age;
    this.salary = salary;
  }
  toString() { return this.salary; }
}

let list = [
    new properties("P", 12, 100),
    new properties("S", 80, 12000),
    new properties("Q", 25, 80000),
    new properties("W", 55, 45000),
    new properties("E", 32, 25000),
    new properties("V", 5, 0)
];

console.log("Min: ", Math.min(...list));

As you are compiling it from a Typescript to JavaScript you can write this code in a more cleaner way.

Now, this will work (without overhead) incase you are constructing the data from your side. Incase you are receiving this data, then each object will not contain this toString method. however you can convert each of them to property class instance, but then this will not give you any additional benifit as still you have to use a map, and then min

Koushik Chatterjee
  • 4,106
  • 3
  • 18
  • 32