【问题标题】:Return object with highest value返回具有最高值的对象
【发布时间】: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


    【解决方案1】:

    你可以做一个简单的一行reduce:

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

    注意:在点击此代码之前,您应该确保 games 不为空。正如 cmets 中所指出的,如果 games 为空,此代码将引发错误。

    (您可以改为向.reduce 提供initial value 以避免错误。但通常最直接的方法是检查games 是否为空,而不是提供虚拟/默认值和然后稍后检查您是否获得了该虚拟/默认值。)

    【讨论】:

    • 感谢您的回答。它按预期工作。快速的问题。在这种情况下,max 是什么?
    • max 始终是迄今为止获得最多票数的游戏。如果game 的票数比max 多,则在下一次迭代中它变为max,以此类推,直到到达数组的末尾。
    • 如果games 为空because 没有为.reduce 提供默认值,则此特定代码将失败。
    【解决方案2】:

    您可以使用Array#map 和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);
    }());

    【讨论】:

    • 不知道优雅,但很短 - 是的。
    【解决方案3】:

    只是迭代,当你找到更大的值时更新最大值和找到它的对象:

    var max = -Infinity, argmax;
    for(var game of games)
      if(game.votes >= max)
        max = game.votes, argmax = game;
    argmax;
    

    【讨论】:

      【解决方案4】:

      根据票数排序呢?

      games.sort( function(a, b){
          return a.votes < b.votes; 
      }); 
      

      现在数组中的第一场比赛得票最多。

      【讨论】:

        猜你喜欢
        • 2015-06-23
        • 2013-09-28
        • 2020-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-28
        • 1970-01-01
        相关资源
        最近更新 更多