我发送 API 请求并得到:
Object { meta: {…}, data: (75) […], dictionaries: {…} }
data: Array(75) [ {…}, {…}, {…}, … ]
0: Object { type: "flight-offer", id: "1", source: "GDS", … }
1: Object { type: "flight-offer", id: "2", source: "GDS", … }
2: Object { type: "flight-offer", id: "3", source: "GDS", … }
3: Object { type: "flight-offer", id: "4", source: "GDS", … }
4: Object { type: "flight-offer", id: "5", source: "GDS", … }
5: Object { type: "flight-offer", id: "6", source: "GDS", … }
6: Object { type: "flight-offer", id: "7", source: "GDS", … }
7: Object { type: "flight-offer", id: "8", source: "GDS", … }
8: Object { type: "flight-offer", id: "9", source: "GDS", … }
9: Object { type: "flight-offer", id: "10", source: "GDS", … }
10: Object { type: "flight-offer", id: "11", source: "GDS", … }
11: Object { type: "flight-offer", id: "12", source: "GDS", … }
12: Object { type: "flight-offer", id: "13", source: "GDS", … }
13: Object { type: "flight-offer", id: "14", source: "GDS", … }
每个物体看起来都像:
0: Object { type: "flight-offer", id: "1", source: "GDS", … }
id: "1"
instantTicketingRequired: false
itineraries: Array [ {…} ]
lastTicketingDate: "2024-04-20"
lastTicketingDateTime: "2024-04-20"
nonHomogeneous: false
numberOfBookableSeats: 9
oneWay: false
price: Object { currency: "EUR", total: "580.13", base: "257.00", … }
pricingOptions: Object { fareType: (1) […], includedCheckedBagsOnly: true }
source: "GDS"
travelerPricings: Array [ {…} ]
type: "flight-offer"
validatingAirlineCodes: Array [ "TK" ]
我想根据价格对结果进行排序。所以我有一个基于价格的所有这些对象的数组,然后我想获得前 5 个结果。
我试过:
this.http.get(url, { headers: this.headers }).subscribe((data: any) => {
this.responseData$ = data;
console.log(data);
const flightOffers = data.data || []; // Assuming flight offers are in `data.data`
// Sort the flight offers by price.total
let sortedResults: any[] = flightOffers.sort(
(offerA: any, offerB: any) => {
// Ensure both offers have a price object and total property
if (
!offerA.price ||
!offerA.price.total ||
!offerB.price ||
!offerB.price.total
) {
console.warn(
'One or more offers lack a valid price object or total property.',
);
return 0; // Maintain original order in case of missing prices
}
return offerA.price.total - offerB.price.total;
},
);
和
this.http.get(url, { headers: this.headers }).subscribe((data: any) => {
this.responseData$ = data;
console.log(data);
let sortedResults = [];
if (Array.isArray(data.data)) {
sortedResults = data.data.sort((a, b) => {
return parseFloat(a.price.total) - parseFloat(b.price.total);
});
}
const firstFiveResults = sortedResults.slice(0, 5);
但它不起作用,我得到了具有相同价格的 5 个元素的数组:总计。怎么排序?
你的第二次尝试应该会成功(重要部分)。我已将您的示例简化为最低限度(数据+排序+切片)。这将输出测试数据集中最便宜的前 5 个。您必须调试其他部分才能找出问题所在(返回的实际数据等)
var data = [{
id: 1,
price: {
total: "580.13"
}
},
{
id: 2,
price: {
total: "570.13"
}
},
{
id: 3,
price: {
total: "560.13"
}
},
{
id: 4,
price: {
total: "550.13"
}
},
{
id: 5,
price: {
total: "540.13"
}
},
{
id: 6,
price: {
total: "530.13"
}
}
];
const sortedResults = data.sort((a, b) => parseFloat(a.price.total) - parseFloat(b.price.total));
console.log(sortedResults.slice(0, 5));