I have a array called games with a value 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 }
]
I want to return the object with the most amount of votes. I've googled and found some methods using Math.max.apply, but it's returning the number of votes, and not the object itself.
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;
};
Any tips on how to return the object with the highest votes?