【发布时间】:2016-04-29 14:36:41
【问题描述】:
我有一个名为 games 的数组,其值为 votes,
let games = [
{ id: 1, name: 'Star Wars: Imperial Assault', company: company.Fantasy_Flight, available: true, category: Category.SciFi, votes: 3},
{ id: 2, name: 'Game of Thrones: Second Edition', company: 'Fantassy Flight', available: false, category: Category.Fantasy, votes: 4 },
{ id: 3, name: 'Merchans and Marauders', company: 'Z-Man Gaming', available: true, category: Category.Pirates, votes: 5 },
{ id: 4, name: 'Eclipse', company: 'Lautapelit', available: false, category: Category.SciFi, votes: 6 },
{ id: 5, name: 'Fure of Dracula', company: 'Fantasy Flight', available: true, category: Category.Fantasy, votes: 2 }
]
我想返回得票最多的对象。我在 Google 上搜索并找到了一些使用 Math.max.apply 的方法,但它返回的是投票数,而不是对象本身。
function selectMostPopular():string {
const allGames = getAllGames();
let mostPopular: string = Math.max.apply(Math, allGames.map(function (o) { return o.votes; }));
console.log(mostPopular);
return mostPopular;
};
关于如何返回得票最高的对象的任何提示?
【问题讨论】:
标签: javascript