【问题标题】:How do I sort this Array of objects using Vanilla or lodash如何使用 Vanilla 或 lodash 对这个对象数组进行排序
【发布时间】:2020-02-08 13:28:21
【问题描述】:

我通过有角度的 HttpClient Observable 将该对象返回给我。

{
"jsonrpc": "2.0",
"result": [
{
  "event": {
    "id": "29688772",
    "name": "Demoliner/Middelkoop v Behar/Escobar",
    "countryCode": "AR",
    "timezone": "UTC",
    "openDate": "2020-02-08T20:00:00.000Z"
  },
  "marketCount": 1
},
{
  "event": {
    "id": "29691591",
    "name": "Bemelmans v Pellegrino",
    "countryCode": "FR",
    "timezone": "UTC",
    "openDate": "2020-02-09T09:00:00.000Z"
  },
  "marketCount": 1
},
{
  "event": {
    "id": "29690566",
    "name": "Diez v Emil Ruusuvuori",
    "countryCode": "NL",
    "timezone": "UTC",
    "openDate": "2020-02-08T13:14:00.000Z"
  },
  "marketCount": 1
},
{
  "event": {
    "id": "29690822",
    "name": "Koepfer v J Rodionov",
    "countryCode": "US",
    "timezone": "UTC",
    "openDate": "2020-02-08T18:00:00.000Z"
  },
  "marketCount": 1
},
{
  "event": {
    "id": "29691586",
    "name": "Basic v Vanni",
    "countryCode": "FR",
    "timezone": "UTC",
    "openDate": "2020-02-09T09:00:00.000Z"
  },
  "marketCount": 1
},
{
  "event": {
    "id": "29691596",
    "name": "P Kotov v Mayot",
    "countryCode": "FR",
    "timezone": "UTC",
    "openDate": "2020-02-09T09:00:00.000Z"
  },
  "marketCount": 1
}

我想按 openDate 对数组进行排序。

我想在订阅中这样做

getTennisMatches() {
this.betfairService.getTennisMatches().subscribe((data: any[]) => {
  this.matches = data;
  this.sortedMatches = this.matches.sort((a, b) => (a.result.event.openDate > b.result.event.openDate) ? 1 : -1);
  // OR
  this.sortedMatches = _.sortBy(this.matches.result, o => o.result.event.openDate);
});

}

两种排序方法都不能处理错误 this.matches.sort is not a function 香草或 Cannot read property 'event' of undefined 为 lodash

我认为我必须迭代事件,但我不确定我在做什么。我使用 lodash 是因为我发现它的语法更容易理解,但我不必使用它。谢谢

【问题讨论】:

  • 数组是this.matches.result,而不是this.matches。而且您的排序比较器函数应该返回一个数字,而不是布尔值。
  • 感谢您的快速回复。我以为它返回的是一个 1 或负 1 的数字。抱歉,我有点困惑。
  • 1.它不返回1-1,而是返回10。 2. 即使这样,您也必须返回 三个 值,而不是两个。了解两个项目是否相等 很重要。 Sorting in JavaScript: Shouldn't returning a boolean be enough for a comparison function?

标签: javascript angular lodash


【解决方案1】:

您可以对对象内的result 数组进行排序,而不是对matches 对象进行排序。

this.matches.result.sort((a, b) => Date.parse(a.event.openDate) - Date.parse(b.event.openDate));

但请注意,这也会对原始对象进行排序。

sn-p 简化示例:

let matches = {
result : [
  { id:1001 , event: { openDate : "2020-02-03T20:00:00.000Z" } },
  { id:1002 , event: { openDate : "2020-02-01T20:00:00.000Z" } },
  { id:1003 , event: { openDate : "2020-02-03T20:00:00.000Z" } },
  { id:1004 , event: { openDate : "2020-02-04T20:00:00.000Z" } },
  { id:1005 , event: { openDate : "2020-02-02T20:00:00.000Z" } }
]
};

let sorted = matches.result.sort((a, b) =>  Date.parse(a.event.openDate) - Date.parse(b.event.openDate));

console.log('original object after sort', JSON.stringify(matches));

【讨论】:

    【解决方案2】:

    this.matches.sort is not a function 表示您的变量 this.matches 是空的,您正在尝试对其进行迭代,因此首先您必须检查您对 MS 调用的响应。可以检索一个空数组。

    type 而不是 'any' 将有助于您检查它是否有效并检索一些数据但它们不具有预期的格式。

    还可以检查 Chrome 开发工具中的“网络”部分以查看响应。

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 1970-01-01
      • 2017-09-08
      • 2023-04-10
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多