我发送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个结果.

我try 过:

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 和 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);

but it didnt work, 和 i get the array with 5 elements with the same price: total. How to sort it?

推荐答案

您的第二次try 应该有效(重要部分).我已经将您的示例归结为最低限度(数据+排序+切片).这将输出测试数据集中最便宜的前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));

Javascript相关问答推荐

如何循环访问对象数组并以关键值形式获得结果?

使用JavaScript重命名对象数组中的键

如何将拖放功能添加到我已自定义为图像的文件输入HTML标签中?

如何通过在提交时工作的函数显示dom元素?

如何修复内容安全策略指令脚本-SRC自身错误?

Plotly热图:在矩形上zoom 后将zoom 区域居中

类型自定义lazy Promise. all

将状态向下传递给映射的子元素

在这种情况下,如何 for each 元素添加id?

屏幕右侧屏障上的产卵点""

Websocket错误—有一个或多个保留位开启:reserved1 = 1,reserved2 = 0,reserved3 = 0

如何通过将实例方法的名称传递给具有正确类型的参数的继承方法来调用实例方法?

在浏览器中触发插入事件时检索编码值的能力

如何发送从REST Api收到的PNG数据响应

TypeError:无法读取未定义的属性(正在读取';宽度';)

SPAN不会在点击时关闭模式,尽管它们可以发送日志(log)等

使用父标签中的Find函数查找元素

有没有办法在R中创建一张具有多个色标的热图?

如何在Firebase中读取对象的特定字段?

Played link-Initialize.js永远显示加载符号