我有一个叫做玩家的阵列。我试图找出阵列中前七名球员的总体基础。我有一个功能,找到最好的球员,但我需要帮助,如何使我找到第二好的,等等。请帮忙!先感谢您。这是代码:
<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 :(得分:1)
更新:Array.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));
&#13;
答案 1 :(得分:1)
回答关于添加可用属性的第二个问题,您可以在DOM中过滤以隐藏它。您可以将map函数链接到sort,以返回已添加可用属性的已排序数组。
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]);