Skip to main content
1 de 2

como puedo encontar el coste minimo es este array de objetos?

E probado varias maneras y los reasultados son NaN O INFINITY o simplemente me devuelve todos los numeros y yo solo quiero el precio menor que seria 90. No se si estoy usando bien el metodo math.min() o si en un array de objetos es mas complicado.

let flights = [

{ id: 00, to: 'Bilbao', from: 'Barcelona', cost: 1600, scale: false },

{ id: 01, to: 'New York', from: 'Barcelona', cost: 700, scale: false },

{ id: 02, to: 'Los Angeles', from: 'Madrid', cost: 1100, scale: true },

{ id: 03, to: 'Paris', from: 'Barcelona', cost: 210, scale: false },

{ id: 04, to: 'Roma', from: 'Barcelona', cost: 150, scale: false },

{ id: 05, to: 'London', from: 'Madrid', cost: 200, scale: false },

{ id: 06, to: 'Madrid', from: 'Barcelona', cost: 90, scale: false },

{ id: 07, to: 'Tokyo', from: 'Madrid', cost: 1500, scale: true },

{ id: 08, to: 'Shangai', from: 'Barcelona', cost: 800, scale: true },

{ id: 09, to: 'Sydney', from: 'Barcelona', cost: 150, scale: true },

{ id: 10, to: 'Tel-Aviv', from: 'Madrid', cost: 150, scale: false }];
function minPrice(){
    document.write('<h1>Vuelo con el precio mas bajo</h1>')
    for (let i = 0; i < flights.length; i++){
    let min = Math.min(flights[i].cost);
    document.write(min);
    console.log(min);
    }
    
  };

  minPrice();
````