我正在对 API 进行 AJAX 调用并获得以下 json(下方)响应。我想从“报价”中获得最低的“最低价格”,但似乎无法找到最佳方法。
我可以遍历报价并将每个最低价格插入一个数组,然后对该数组进行排序以获得最低值,但我还想要特定报价的额外信息,即它是“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
}]
}
最佳答案
你必须使用 Array.sort()
接受一个 callback
函数作为参数的函数。
var minPrices=obj.Quotes.sort(function(item1,item2){
return item1.MinPrice-item2.MinPrice;
});
一旦我们对 quotes
进行排序,minPrices[0]
将返回具有较低 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]);
如果您只想获取MinPrice
,您应该使用map
函数以便从Quotes< 获取所有
。 MinPrice
/
然后,您必须使用 sort()
函数对数组进行排序
,该函数接受一个 callback
函数作为参数。
arr.sort(function(a,b){
return a-b;
});
最后一步是获取排序数组的第一个元素
,代表最低
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]);
关于javascript - API JSON 响应 选择最低价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42000896/