API JSON响应选择最低价格

时间:2017-02-02 11:21:41

标签: javascript jquery arrays json

我正在对API进行AJAX调用并获得以下json(下面)响应。我想要获得最低的MinPrice'来自' Quotes',但似乎无法找出最佳方法。

我可以遍历引号并将每个minprice推送到一个数组中,然后对该数组进行排序以获得最低值,但我也希望该特定引号提供额外信息,即它是' outboundleg&#39 ;

{
    "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
    }]
}

3 个答案:

答案 0 :(得分: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函数从MinPrice获取所有Quotes

然后,您必须使用sort数组sort()数组接受callback函数作为参数。

arr.sort(function(a,b){
   return a-b;
});

最后一步是获取排序数组的第一个element,它代表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]);




答案 1 :(得分:1)

由于Quotes是一个数组,Array.sort()基于MinPrice属性,然后您可以在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])

答案 2 :(得分:0)

您可以使用sort函数对对象数组进行排序。

exception falcon.HTTPMovedPermanently(location)