js实现取出数组中的最大数和最小数

    var data = [
      { player: 'player1', score: 5 },
      { player: 'player2', score: 6 },
      { player: 'player3', score: 4 },
      { player: 'player4', score: 8 },
      { player: 'player5', score: 6 },
      { player: 'player6', score: 9 },
      { player: 'player7', score: 7 },
      { player: 'player8', score: 2 },
      { player: 'player9', score: 6 },
      { player: 'player10', score: 9 },
    ]
    function compare(a, b) {
      return b.score - a.score;
    }
    var newdata = data.sort(compare);
    alert('最大数:' + newdata[0].score);
    alert('最小数:' + newdata[9].score);


或者
    data.sort(function (x, y) {
      return y.score - x.score
    })
    alert('最大数:' + data[0].score);
    alert('最小数:' + data[9].score);

  可以根据不同情况进行变更。

原文地址:https://www.cnblogs.com/bertha-zm/p/8509333.html