2016-08-06 10 views
0

私はplayersという配列を持っています。私は配列の上位7人のプレイヤーが全体的に何に基づいているのかを見出そうとしています。私は最高のプレイヤーを見つける機能を持っていますが、私は2番目のベストを見つけることができる場所などを作る方法について助けが必要です。助けてください!前もって感謝します。アレイで2番目に優れたものを見つけるにはどうすればよいですか?

<script> 
      var players = [ 
       {name: "Pat Moran", overall: 67, position: "AP"}, 
       {name: "Peter Webb", overall: 81, position: "AP"}, 
       {name: "Ramiro Ramirez", overall: 74, position: "AP"}, 
       {name: "Manuel Knight", overall: 68, position: "RP"}, 
       {name: "Allan Alexander", overall: 71, position: "RP"}, 
       {name: "Gerald Bowman", overall: 81, position: "RP"}, 
       {name: "Owen Day", overall: 78, position: 3}, 
       {name: "Jean Ford", overall: 70, position: 3}, 
       {name: "Guy Curtis", overall: 59, position: 3}, 
       {name: "Dallas Diaz", overall: 68, position: 4}, 
       {name: "Jesus Brooks", overall: 70, position: 4}, 
       {name: "Todd Beck", overall: 76, position: 4}, 
       {name: "Steve Cortez", overall: 66, position: 5}, 
       {name: "Toby Caldwell", overall: 64, position: 5}, 
       {name: "Seth Bradley", overall: 73, position: 5}, 
       {name: "Lawrence McCarthy", overall: 67, position: 6}, 
       {name: "Gerardo Marsh", overall: 77, position: 6}, 
       {name: "Alvin Nash", overall: 78, position: 6} 
      ]; 

      function findBestPlayer() { 
       var bestSoFar = 0; 
       var bestPlayer; 
       for (var i = 0; i < players.length; i++) { 
        if (players[i].overall > bestSoFar) { 
         bestPlayer = players[i]; 
         bestSoFar = players[i].overall; 
        } 
       } 
       return bestPlayer; 
      } 

      var bestPlayer = findBestPlayer(); 

      document.getElementById("p1").innerHTML = bestPlayer.name; 
      document.getElementById("p1ovr").innerHTML = bestPlayer.overall; 
      document.getElementById("p1pos").innerHTML = bestPlayer.position; 
     </script> 
+0

ソートしないでください。 – Redu

答えて

1

UPDATEArray.filterが利用できるプレーヤーでのみ確認することができますここでは、コードです。

コンパレータ機能を使用してソートすることができます。

function comp(b, a) { 
 
    return a.overall - b.overall; 
 
} 
 
var players = [{ 
 
    name: "Pat Moran", 
 
    overall: 67, 
 
    position: "AP", 
 
    available: true 
 
}, { 
 
    name: "Peter Webb", 
 
    overall: 81, 
 
    position: "AP", 
 
    available: false 
 
}, { 
 
    name: "Ramiro Ramirez", 
 
    overall: 74, 
 
    position: "AP", 
 
    available: true 
 
}, { 
 
    name: "Manuel Knight", 
 
    overall: 68, 
 
    position: "RP", 
 
    available: false 
 
}, { 
 
    name: "Allan Alexander", 
 
    overall: 71, 
 
    position: "RP", 
 
    available: true 
 
}, { 
 
    name: "Gerald Bowman", 
 
    overall: 81, 
 
    position: "RP", 
 
    available: false 
 
}, { 
 
    name: "Owen Day", 
 
    overall: 78, 
 
    position: 3, 
 
    available: true 
 
}, { 
 
    name: "Jean Ford", 
 
    overall: 70, 
 
    position: 3, 
 
    available: false 
 
}, { 
 
    name: "Guy Curtis", 
 
    overall: 59, 
 
    position: 3, 
 
    available: true 
 
}, { 
 
    name: "Dallas Diaz", 
 
    overall: 68, 
 
    position: 4, 
 
    available: false 
 
}, { 
 
    name: "Jesus Brooks", 
 
    overall: 70, 
 
    position: 4, 
 
    available: true 
 
}, { 
 
    name: "Todd Beck", 
 
    overall: 76, 
 
    position: 4, 
 
    available: false 
 
}, { 
 
    name: "Steve Cortez", 
 
    overall: 66, 
 
    position: 5, 
 
    available: true 
 
}, { 
 
    name: "Toby Caldwell", 
 
    overall: 64, 
 
    position: 5, 
 
    available: false 
 
}, { 
 
    name: "Seth Bradley", 
 
    overall: 73, 
 
    position: 5, 
 
    available: true 
 
}, { 
 
    name: "Lawrence McCarthy", 
 
    overall: 67, 
 
    position: 6, 
 
    available: false 
 
}, { 
 
    name: "Gerardo Marsh", 
 
    overall: 77, 
 
    position: 6, 
 
    available: true 
 
}, { 
 
    name: "Alvin Nash", 
 
    overall: 78, 
 
    position: 6, 
 
    available: false 
 
}]; 
 

 
function checkAvailabilty(obj) { 
 
    return !!obj.available; 
 
} 
 

 
var findBestPlayer = (function(arr) { 
 
    // a copy of the original data is made. 
 
    var data = arr.slice(0); 
 
    // sort the data with a comparator function 
 
    data.sort(comp); 
 
    // filter out the unavailable players. 
 
    data = data.filter(checkAvailabilty); 
 
    return function(index) { 
 
    return data[index]; 
 
    } 
 
})(players); 
 
// best player. 
 
console.log('best available player: ', findBestPlayer(0)); 
 
// second best player. 
 
console.log('second best available player: ', findBestPlayer(1));

+0

ありがとう、これは多くの助けになりました。私は、各プレイヤーに "利用可能"という変数を追加します。利用可能な場合はfalse、私はそれが関数に含まれないようにします。したがって、画面から消えて、次のベストプレーヤーが表示されます。これを行う方法はありますか? –

+0

ちょっと@DallasWhite絶対に可能です。あなたはそれを達成するためにarray.filterを使うことができます。更新された回答を確認してください。 – Ayan

+1

ありがとう、それは働いた! –

1

あなたが非表示にDOMでのフィルタリングができ、利用できるプロパティの追加についてあなたの2番目の質問に答えるために。並べ替えにマップ関数を連鎖させて、利用可能なプロパティが追加されたソートされた配列を返すことができます。

function comp (b,a) { 
    return a.overall - b.overall; 
} 
var players = [ 
    {name: "Pat Moran", overall: 67, position: "AP"}, 
    {name: "Peter Webb", overall: 81, position: "AP"}, 
    {name: "Ramiro Ramirez", overall: 74, position: "AP"}, 
    {name: "Manuel Knight", overall: 68, position: "RP"}, 
    {name: "Allan Alexander", overall: 71, position: "RP"}, 
    {name: "Gerald Bowman", overall: 81, position: "RP"}, 
    {name: "Owen Day", overall: 78, position: 3}, 
    {name: "Jean Ford", overall: 70, position: 3}, 
    {name: "Guy Curtis", overall: 59, position: 3}, 
    {name: "Dallas Diaz", overall: 68, position: 4}, 
    {name: "Jesus Brooks", overall: 70, position: 4}, 
    {name: "Todd Beck", overall: 76, position: 4}, 
    {name: "Steve Cortez", overall: 66, position: 5}, 
    {name: "Toby Caldwell", overall: 64, position: 5}, 
    {name: "Seth Bradley", overall: 73, position: 5}, 
    {name: "Lawrence McCarthy", overall: 67, position: 6} 
    {name: "Gerardo Marsh", overall: 77, position: 6}, 
    {name: "Alvin Nash", overall: 78, position: 6} 
]; 

var findBestPlayer = players.sort(comp).map(function(player, index){ 
    player.available = index <= 6 ? true : false 
    return player 
}) 

// best player. 
console.log(findBestPlayer[0]); 

// second best player. 
console.log(findBestPlayer[1]); 
関連する問題