I'm making an AJAX call to an API and getting the following json (below) response. I'm wanting to get the lowest 'MinPrice' from 'Quotes', but can't seem to figure out the best approach.
I can loop through the quotes and push each minprice into an array, and then sort that array to get the lowest value, but I also want that specific quotes extra information i.e it's 'outboundleg'.
{
"Quotes": [{
"QuoteId": 1,
"MinPrice": 70.0,
"Direct": true,
"OutboundLeg": {
"CarrierIds": [1047],
"OriginId": 84892,
"DestinationId": 65698,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-02-01T01:05:00"
}, {
"QuoteId": 2,
"MinPrice": 85.0,
"Direct": true,
"InboundLeg": {
"CarrierIds": [1047],
"OriginId": 65698,
"DestinationId": 84892,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-02-01T04:23:00"
}, {
"QuoteId": 3,
"MinPrice": 86.0,
"Direct": true,
"InboundLeg": {
"CarrierIds": [1047],
"OriginId": 65698,
"DestinationId": 84892,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-02-01T01:57:00"
}, {
"QuoteId": 4,
"MinPrice": 164.0,
"Direct": true,
"OutboundLeg": {
"CarrierIds": [1047],
"OriginId": 84892,
"DestinationId": 65698,
"DepartureDate": "2017-02-13T00:00:00"
},
"InboundLeg": {
"CarrierIds": [1047],
"OriginId": 65698,
"DestinationId": 84892,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-01-30T21:03:00"
}],
"Places": [{
"PlaceId": 65698,
"IataCode": "LHR",
"Name": "London Heathrow",
"Type": "Station",
"SkyscannerCode": "LHR",
"CityName": "London",
"CityId": "LOND",
"CountryName": "United Kingdom"
}, {
"PlaceId": 84892,
"IataCode": "TXL",
"Name": "Berlin Tegel",
"Type": "Station",
"SkyscannerCode": "TXL",
"CityName": "Berlin",
"CityId": "BERL",
"CountryName": "Germany"
}],
"Carriers": [{
"CarrierId": 838,
"Name": "Air France"
}, {
"CarrierId": 881,
"Name": "British Airways"
}, {
"CarrierId": 1047,
"Name": "eurowings"
}, {
"CarrierId": 1218,
"Name": "Iberia"
}, {
"CarrierId": 1324,
"Name": "KLM"
}, {
"CarrierId": 1368,
"Name": "Lufthansa"
}, {
"CarrierId": 1384,
"Name": "Swiss"
}, {
"CarrierId": 1707,
"Name": "SAS"
}, {
"CarrierId": 1710,
"Name": "Brussels Airlines"
}],
"Currencies": [{
"Code": "GBP",
"Symbol": "£",
"ThousandsSeparator": ",",
"DecimalSeparator": ".",
"SymbolOnLeft": true,
"SpaceBetweenAmountAndSymbol": false,
"RoundingCoefficient": 0,
"DecimalDigits": 2
}]
}
You have to use Array.sort() function which accept as argument a callback function.
var minPrices=obj.Quotes.sort(function(item1,item2){
return item1.MinPrice-item2.MinPrice;
});
Once we sorted the quotes, minPrices[0] returns the object with the lowers MinPrice.
var obj={
"Quotes": [{
"QuoteId": 1,
"MinPrice": 70.0,
"Direct": true,
"OutboundLeg": {
"CarrierIds": [1047],
"OriginId": 84892,
"DestinationId": 65698,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-02-01T01:05:00"
}, {
"QuoteId": 2,
"MinPrice": 85.0,
"Direct": true,
"InboundLeg": {
"CarrierIds": [1047],
"OriginId": 65698,
"DestinationId": 84892,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-02-01T04:23:00"
}, {
"QuoteId": 3,
"MinPrice": 86.0,
"Direct": true,
"InboundLeg": {
"CarrierIds": [1047],
"OriginId": 65698,
"DestinationId": 84892,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-02-01T01:57:00"
}, {
"QuoteId": 4,
"MinPrice": 164.0,
"Direct": true,
"OutboundLeg": {
"CarrierIds": [1047],
"OriginId": 84892,
"DestinationId": 65698,
"DepartureDate": "2017-02-13T00:00:00"
},
"InboundLeg": {
"CarrierIds": [1047],
"OriginId": 65698,
"DestinationId": 84892,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-01-30T21:03:00"
}],
"Places": [{
"PlaceId": 65698,
"IataCode": "LHR",
"Name": "London Heathrow",
"Type": "Station",
"SkyscannerCode": "LHR",
"CityName": "London",
"CityId": "LOND",
"CountryName": "United Kingdom"
}, {
"PlaceId": 84892,
"IataCode": "TXL",
"Name": "Berlin Tegel",
"Type": "Station",
"SkyscannerCode": "TXL",
"CityName": "Berlin",
"CityId": "BERL",
"CountryName": "Germany"
}],
"Carriers": [{
"CarrierId": 838,
"Name": "Air France"
}, {
"CarrierId": 881,
"Name": "British Airways"
}, {
"CarrierId": 1047,
"Name": "eurowings"
}, {
"CarrierId": 1218,
"Name": "Iberia"
}, {
"CarrierId": 1324,
"Name": "KLM"
}, {
"CarrierId": 1368,
"Name": "Lufthansa"
}, {
"CarrierId": 1384,
"Name": "Swiss"
}, {
"CarrierId": 1707,
"Name": "SAS"
}, {
"CarrierId": 1710,
"Name": "Brussels Airlines"
}],
"Currencies": [{
"Code": "GBP",
"Symbol": "£",
"ThousandsSeparator": ",",
"DecimalSeparator": ".",
"SymbolOnLeft": true,
"SpaceBetweenAmountAndSymbol": false,
"RoundingCoefficient": 0,
"DecimalDigits": 2
}]
}
var minPrices=obj.Quotes.sort(function(item1,item2){
return item1.MinPrice-item2.MinPrice;
});
console.log(minPrices[0]);
If you want to obtain only the MinPrice, you should use map function in order to obtain all MinPrice from Quotes.
Then, you have to sort array using sort() function which accept as argument a callback function.
arr.sort(function(a,b){
return a-b;
});
The last step is to get the first element of sorted array, which represents the lowest MinPrice.
var obj={
"Quotes": [{
"QuoteId": 1,
"MinPrice": 70.0,
"Direct": true,
"OutboundLeg": {
"CarrierIds": [1047],
"OriginId": 84892,
"DestinationId": 65698,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-02-01T01:05:00"
}, {
"QuoteId": 2,
"MinPrice": 85.0,
"Direct": true,
"InboundLeg": {
"CarrierIds": [1047],
"OriginId": 65698,
"DestinationId": 84892,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-02-01T04:23:00"
}, {
"QuoteId": 3,
"MinPrice": 86.0,
"Direct": true,
"InboundLeg": {
"CarrierIds": [1047],
"OriginId": 65698,
"DestinationId": 84892,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-02-01T01:57:00"
}, {
"QuoteId": 4,
"MinPrice": 164.0,
"Direct": true,
"OutboundLeg": {
"CarrierIds": [1047],
"OriginId": 84892,
"DestinationId": 65698,
"DepartureDate": "2017-02-13T00:00:00"
},
"InboundLeg": {
"CarrierIds": [1047],
"OriginId": 65698,
"DestinationId": 84892,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-01-30T21:03:00"
}],
"Places": [{
"PlaceId": 65698,
"IataCode": "LHR",
"Name": "London Heathrow",
"Type": "Station",
"SkyscannerCode": "LHR",
"CityName": "London",
"CityId": "LOND",
"CountryName": "United Kingdom"
}, {
"PlaceId": 84892,
"IataCode": "TXL",
"Name": "Berlin Tegel",
"Type": "Station",
"SkyscannerCode": "TXL",
"CityName": "Berlin",
"CityId": "BERL",
"CountryName": "Germany"
}],
"Carriers": [{
"CarrierId": 838,
"Name": "Air France"
}, {
"CarrierId": 881,
"Name": "British Airways"
}, {
"CarrierId": 1047,
"Name": "eurowings"
}, {
"CarrierId": 1218,
"Name": "Iberia"
}, {
"CarrierId": 1324,
"Name": "KLM"
}, {
"CarrierId": 1368,
"Name": "Lufthansa"
}, {
"CarrierId": 1384,
"Name": "Swiss"
}, {
"CarrierId": 1707,
"Name": "SAS"
}, {
"CarrierId": 1710,
"Name": "Brussels Airlines"
}],
"Currencies": [{
"Code": "GBP",
"Symbol": "£",
"ThousandsSeparator": ",",
"DecimalSeparator": ".",
"SymbolOnLeft": true,
"SpaceBetweenAmountAndSymbol": false,
"RoundingCoefficient": 0,
"DecimalDigits": 2
}]
}
var minPrices=obj.Quotes.map(function(item){
return item.MinPrice;
}).sort(function(a,b){return a-b});
console.log(minPrices[0]);
As Quotes is an array, Array.sort() it on the basis of MinPrice propert and then you can extract information on the basis of index.
var sorted = data.Quotes.sort((a, b) => {return a.MinPrice - b.MinPrice})
console.log(sorted[0])
var data = {
"Quotes": [{
"QuoteId": 1,
"MinPrice": 70.0,
"Direct": true,
"OutboundLeg": {
"CarrierIds": [1047],
"OriginId": 84892,
"DestinationId": 65698,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-02-01T01:05:00"
}, {
"QuoteId": 2,
"MinPrice": 85.0,
"Direct": true,
"InboundLeg": {
"CarrierIds": [1047],
"OriginId": 65698,
"DestinationId": 84892,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-02-01T04:23:00"
}, {
"QuoteId": 3,
"MinPrice": 86.0,
"Direct": true,
"InboundLeg": {
"CarrierIds": [1047],
"OriginId": 65698,
"DestinationId": 84892,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-02-01T01:57:00"
}, {
"QuoteId": 4,
"MinPrice": 164.0,
"Direct": true,
"OutboundLeg": {
"CarrierIds": [1047],
"OriginId": 84892,
"DestinationId": 65698,
"DepartureDate": "2017-02-13T00:00:00"
},
"InboundLeg": {
"CarrierIds": [1047],
"OriginId": 65698,
"DestinationId": 84892,
"DepartureDate": "2017-02-13T00:00:00"
},
"QuoteDateTime": "2017-01-30T21:03:00"
}],
"Places": [{
"PlaceId": 65698,
"IataCode": "LHR",
"Name": "London Heathrow",
"Type": "Station",
"SkyscannerCode": "LHR",
"CityName": "London",
"CityId": "LOND",
"CountryName": "United Kingdom"
}, {
"PlaceId": 84892,
"IataCode": "TXL",
"Name": "Berlin Tegel",
"Type": "Station",
"SkyscannerCode": "TXL",
"CityName": "Berlin",
"CityId": "BERL",
"CountryName": "Germany"
}],
"Carriers": [{
"CarrierId": 838,
"Name": "Air France"
}, {
"CarrierId": 881,
"Name": "British Airways"
}, {
"CarrierId": 1047,
"Name": "eurowings"
}, {
"CarrierId": 1218,
"Name": "Iberia"
}, {
"CarrierId": 1324,
"Name": "KLM"
}, {
"CarrierId": 1368,
"Name": "Lufthansa"
}, {
"CarrierId": 1384,
"Name": "Swiss"
}, {
"CarrierId": 1707,
"Name": "SAS"
}, {
"CarrierId": 1710,
"Name": "Brussels Airlines"
}],
"Currencies": [{
"Code": "GBP",
"Symbol": "£",
"ThousandsSeparator": ",",
"DecimalSeparator": ".",
"SymbolOnLeft": true,
"SpaceBetweenAmountAndSymbol": false,
"RoundingCoefficient": 0,
"DecimalDigits": 2
}]
};
var sorted = data.Quotes.sort((a, b) => {return a.MinPrice - b.MinPrice})
console.log(sorted[0])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With