按属性排序嵌套对象不起作用

时间:2019-04-11 01:15:04

标签: javascript sorting

我正在尝试通过对象players内的average实例的player属性对对象进行排序,但是我无法访问以使其起作用。

这是我登录时Players对象的外观:

[ Player { player_id: 1, scores: [ 50 ], total: 50, average: 50 },
  Player { player_id: 2, scores: [ 80, 70, 60 ], total: 210, average: 80 },
  Player { player_id: 3, scores: [ 90, 85 ], total: 175, average: 90 } ]

但是我的排序功能似乎很平稳-我想我需要在某个地方添加player才能访问嵌套对象。

这是功能代码:

/* class definition excluded */

    top = (num_players) => 
      Object.values(this.players)
        .sort((a, b) => (a.average - b.average))
        .slice(0, num_players);
};

为此功能编写的测试返回以下内容:

Top 3 [[object Object],[object Object],[object Object]] should equal [3, 2, 1]:

对于上下文,这里是带有测试用例的整个脚本,以防您想尝试一下:

class Player {
  constructor(player_id, score) {
    this.player_id = player_id;
    this.scores = [score];
    this.total = score;
    this.average = this.averageScore();
  }

  addScore(score) {
    this.total += score;
    this.scores.push(score);
    return score;
  }

  averageScore() {
    return this.scores.length ? this.total / this.scores.length : 0;
  }

  resetScore() {
    this.scores = [];
    this.score = 0;
  }

};

class LeaderBoard {

  constructor() {
    this.players = {};
  }

  add_score(player_id, score) {
    if (!this.players[player_id]) {
      this.players[player_id] = new Player(player_id, score);
    } else {
      this.players[player_id].addScore(score);
    }
    var average = this.players[player_id].averageScore();
    return average.toFixed(1);
  }

  top = (num_players) =>
    // console.log(Object.values(this.players);
    Object.values(this.players)
    .sort((a, b) => (a.average - b.average))
    .slice(0, num_players);

  reset = (player_id) => {
    this.players[player_id].resetScore();
  }
};

// Test code here

function array_equals(a, b) {
  if (a === b) return true;
  if (a == null || b == null) return false;
  if (a.length != b.length) return false;
  for (var i = 0; i < a.length; ++i) {
    if (a[i] !== b[i]) return false;
  }
  return true;
}

var leader_board = new LeaderBoard()

leader_board.add_score(1, 50)
console.log(leader_board.add_score(2, 80) == 80)
console.log(leader_board.add_score(2, 70) == 75)
console.log(leader_board.add_score(2, 60) == 70)
console.log('Add score should return the average. test with 1 score')
console.log(leader_board.add_score(3, 90) == 90)
console.log('Add score should return the average. test with 2 scores')
console.log(leader_board.add_score(3, 85) == 87.5)
console.log('Top 3 [' + leader_board.top(3) + '] should equal [3, 2, 1]:')
console.log(array_equals(leader_board.top(3), [3, 2, 1]))
console.log('Top 2 [' + leader_board.top(2) + '] should equal [3, 2]:')
console.log(array_equals(leader_board.top(2), [3, 2]))
leader_board.reset(3)
console.log('After reset top 3 [' + leader_board.top(3) + '] should equal [2, 1, 3]')
console.log(array_equals(leader_board.top(3), [2, 1, 3]))

我希望top将能够按players的顺序对average的对象进行排序,然后将它们从0 to num_players的数组中返回,但是无法成功排序。

到目前为止我还没有运气-如何按嵌套属性排序?

1 个答案:

答案 0 :(得分:3)

  1. 您正在将Player对象与对象本身的属性(player_id)进行比较。 正确的assert语句将如下所示:
console.log(array_equals(leader_board.top(3).map(p => p.player_id), [3, 2, 1]))
  1. 您的排序功能方向错误,只需将其切换为:
    sort((a, b) => (b.average - a.average))