Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return object with highest value

Tags:

javascript

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?

like image 598
Peter Boomsma Avatar asked Apr 29 '16 14:04

Peter Boomsma


People also ask

How do you find the key of the highest value in an object?

To get key with the highest value from object with JavaScript, we can use the Objec. entries method. const data = { a: 1, b: 2, undefined: 3 }; const maxValue = Object.


2 Answers

You can do a simple one-line reduce:

let maxGame = games.reduce((max, game) => max.votes > game.votes ? max : game);

Note: You should make sure games is not empty before you hit this code. As pointed out in the comments, this code will throw an error if games is empty.

(You could instead provide an initial value to .reduce to avoid the error. But it'll usually be most straightforward to check whether games is empty to begin with, rather than provide a dummy/default value and then later checking whether you got that dummy/default value.)

like image 170
andyk Avatar answered Oct 08 '22 01:10

andyk


You can use Array#map and Array#find

// First, get the max vote from the array of objects
var maxVotes = Math.max(...games.map(e => e.votes));

// Get the object having votes as max votes
var obj = games.find(game => game.votes === maxVotes);

(function () {
    var 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
    }];

    var maxVotes = Math.max(...games.map(e => e.votes));
    var obj = games.find(game => game.votes === maxVotes);
    console.log(obj);

    document.body.innerHTML = '<pre>' + JSON.stringify(obj, 0, 4);
}());
like image 43
Tushar Avatar answered Oct 08 '22 00:10

Tushar