根据数组对对象数组进行排序

时间:2019-09-23 06:49:40

标签: javascript arrays ecmascript-6

我想按最嵌套的值(即“结果”)和“ gameType”对给定数组的顶级对象进行排序。按“ gameType”排序由用户设置,用户按特定顺序输入值数组,例如[201、202、249](输入的长度不固定)。超出用户指定范围的“ gameType”将被忽略并推送到结果的末尾。通过根据“结果”进行排序是通过从可用结果中选择最大价值来实现的。

以下是对结果进行排序的功能:

outcomes => Math.max(...outcomes.sort(({ outcome: a }, { outcome: b }) => a - b).map(({ outcome }) => outcome))

这是数组:

[
  {
    "eventId": 513221,
    "games": [
      {
        "gameId": 31313404,
        "gameType": 240,
        "outcomes": [
          {
            "outcome": 1.8
          }, {
            "outcome": 21
          }, {
            "outcome": 2
          }
        ]
      }, {
        "gameId": 31313413,
        "gameType": 201,
        "outcomes": [
          {
            "outcome": 2.1
          }, {
            "outcome": 2.6
          }, {
            "outcome": 4.1
          }
        ]
      }
    ]
  }, {
    "eventId": 513216,
    "games": [
      {
        "gameId": 31314456,
        "gameType": 249,
        "outcomes": [
          {
            "outcome": 1.45
          }, {
            "outcome": 40
          }, {
            "outcome": 2.5
          }
        ]
      }, {
        "gameId": 31314475,
        "gameType": 201,
        "outcomes": [
          {
            "outcome": 1.5
          }, {
            "outcome": 4.55
          }, {
            "outcome": 4.75
          }
        ]
      }
    ]
  }, {
    "eventId": 513263,
    "games": [
      {
        "gameId": 31314398,
        "gameType": 241,
        "outcomes": [
          {
            "outcome": 1.52
          }, {
            "outcome": 2.35
          }
        ]
      }, {
        "gameId": 31314423,
        "gameType": 201,
        "outcomes": [
          {
            "outcome": 1.65
          }, {
            "outcome": 3.9
          }, {
            "outcome": 4.25
          }
        ]
      }
    ]
  }, {
    "eventId": 5132631,
    "games": [
      {
        "gameId": 313143981,
        "gameType": 240,
        "outcomes": [
          {
            "outcome": 1.52
          }, {
            "outcome": 30
          }, {
            "outcome": 2.35
          }
        ]
      }, {
        "gameId": 313144231,
        "gameType": 202,
        "outcomes": [
          {
            "outcome": 1.65
          }, {
            "outcome": 3.9
          }
        ]
      }
    ]
  }, {
    "eventId": 5132632,
    "games": [
      {
        "gameId": 313143912,
        "gameType": 298,
        "outcomes": [
          {
            "outcome": 1.57
          }, {
            "outcome": 2.2
          }
        ]
      }, {
        "gameId": 313144232,
        "gameType": 202,
        "outcomes": [
          {
            "outcome": 1.65
          }, {
            "outcome": 4.11
          }
        ]
      }
    ]
  }
]

1 个答案:

答案 0 :(得分:0)

您可以将一个对象用作排序的辅助对象。这些属性是每个游戏所需的gameType的索引和max的{​​{1}}值。

outcome
var data = [{ eventId: 513221, games: [{ gameId: 31313404, gameType: 240, outcomes: [{ outcome: 1.8 }, { outcome: 21 }, { outcome: 2 }] }, { gameId: 31313413, gameType: 201, outcomes: [{ outcome: 2.1 }, { outcome: 2.6 }, { outcome: 4.1 }] }] }, { eventId: 513216, games: [{ gameId: 31314456, gameType: 249, outcomes: [{ outcome: 1.45 }, { outcome: 40 }, { outcome: 2.5 }] }, { gameId: 31314475, gameType: 201, outcomes: [{ outcome: 1.5 }, { outcome: 4.55 }, { outcome: 4.75 }] }] }, { eventId: 513263, games: [{ gameId: 31314398, gameType: 241, outcomes: [{ outcome: 1.52 }, { outcome: 2.35 }] }, { gameId: 31314423, gameType: 201, outcomes: [{ outcome: 1.65 }, { outcome: 3.9 }, { outcome: 4.25 }] }] }, { eventId: 5132631, games: [{ gameId: 313143981, gameType: 240, outcomes: [{ outcome: 1.52 }, { outcome: 30 }, { outcome: 2.35 }] }, { gameId: 313144231, gameType: 202, outcomes: [{ outcome: 1.65 }, { outcome: 3.9 }] }] }, { eventId: 5132632, games: [{ gameId: 313143912, gameType: 298, outcomes: [{ outcome: 1.57 }, { outcome: 2.2 }] }, { gameId: 313144232, gameType: 202, outcomes: [{ outcome: 1.65 }, { outcome: 4.11 }] }] }],
    input = [201, 202],
    order = data.reduce((o, { eventId, games }) => {
        o[eventId] = games
            .map(({ gameType, outcomes }) => ({ index: input.indexOf(gameType), max: Math.max(...outcomes.map(({ outcome }) => outcome)) }))
            .filter(({ index }) => index !== -1)
            .reduce((a, b) => a.index < b.index ? a : b, { index: Infinity, max: -Infinity });
        return o;
    }, {});

data.sort((a, b) =>
    order[a.eventId].index - order[b.eventId].index ||
    order[b.eventId].max - order[a.eventId].max
);

console.log(data);