在不超过某个值的对象数组中找到最高值 - Javascript

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在不超过某个值的对象数组中找到最高值 - Javascript相关的知识,希望对你有一定的参考价值。

我有一系列对象,想要找到得分最高的玩家,也不超过一定的分数(例如20分)。我该怎么办?

    let playerScores = [ 
  { player: 'Dealer',
    score: 19,
    cards: 'A of ♥, 5 of ♦' },
  { player: 'Player1',
    score: 18,
    cards: '10 of ♦, 8 of ♦' },
  { player: 'Player2',
    score: 23,
    cards: '6 of ♦, 6 of ♥, J of ♠' }
    ]

console.log(playerScores)
// iterates through playerScores
for (let i in playerScores) {
    if (playerScores[i].score > 20) {
        console.log(playerScores[i].player + ' BUSTED!')
        // removes the object with a score over 21 from the array
        playerScores.splice(i,1)
    }
    return playerScores
}
答案

您可以先过滤数据,按分数对其进行排序,然后选择第一个:

let filteredData = playerScores.filter(p=>p.score<=20).sort((a,b) => b.score-a.score);
let result = filteredData.length ? filteredData[0] : undefined;`
另一答案

一个简单的解决方案是创建一个获取极限分数并迭代palyerScores元素的函数,并在下面的示例中查找匹配元素

let playerScores = [
    {
        player: 'Dealer',
        score: 19,
        cards: 'A of ♥, 5 of ♦',
    },
    {
        player: 'Player1',
        score: 18,
        cards: '10 of ♦, 8 of ♦',
    },
    {
        player: 'Player2',
        score: 23,
        cards: '6 of ♦, 6 of ♥, J of ♠',
    },
];
function getHighestScore(limitScore) {
    let highestScore = 0;
    let player = {};
    for (let i = 0; i < playerScores.length; i++) {
        if (
            playerScores[i].score > highestScore &&
            playerScores[i].score < limitScore
        ) {
            highestScore = playerScores[i].score;
            player = Object.assign({}, playerScores[i]);
        }
    }
    return player;
}
let highsetScorePlayerNotExced20 = getHighestScore(20);
console.log(highsetScorePlayerNotExced20);
另一答案

您可以使用链式检查条件并在必要时将对象添加到结果集中,或者将对象替换为具有更高分数的对象。

var playerScores = [{ player: 'Dealer', score: 19, cards: 'A of ♥, 5 of ♦' }, { player: 'Player1', score: 18, cards: '10 of ♦, 8 of ♦' }, { player: 'Player2', score: 23, cards: '6 of ♦, 6 of ♥, J of ♠' }],
    value = 20,
    result = playerScores.reduce(function (r, o) {
        if (o.score >= value) {
            return r;
        }
        if (!r.length || r[0].score < o.score) {
            return [o];
        }
        if (r[0].score === o.score) {
            r.push(o);
        }
        return r;
    }, []);

console.log(result);
另一答案
let playerScores = [
    { player: 'Dealer',
        score: 19,
        cards: 'A of ♥, 5 of ♦' },
    { player: 'Player1',
        score: 18,
        cards: '10 of ♦, 8 of ♦' },
    { player: 'Player2',
        score: 23,
        cards: '6 of ♦, 6 of ♥, J of ♠' }
];

const maxLimit = 20;

const maxScorePlayer = playerScores.reduce((prev, current) => {
    if (current.score >= maxLimit) {
        return prev;
    }

    return (prev.score > current.score) ? prev : current
}, { });

您可以查看reduce函数如何工作here

另一答案

    let playerScores = [ 
      { player: 'Dealer',
        score: 19,
        cards: 'A of ♥, 5 of ♦' },
      { player: 'Player1',
        score: 18,
        cards: '10 of ♦, 8 of ♦' },
      { player: 'Player2',
        score: 20,
        cards: '6 of ♦, 6 of ♥, J of ♠' }
    ];

    var highestScore = Math.max.apply(Math,playerScores.map(function(o){
                                         score = 0;
                                         if(o.score != 20) {
                                             score = o.score;
                                         }
                                         return score;
                                     }));
                         
    console.log(highestScore);

以上是关于在不超过某个值的对象数组中找到最高值 - Javascript的主要内容,如果未能解决你的问题,请参考以下文章

QByteArray数组的单个元素值的判断

数组对象修改某一个属性值的数据类型

JavaScript判断数组对象是否含有某个值的方法(6种)

从多个对象数组中返回具有最高值的对象

如何有效地找到从每个值到下一个较低/较高值的距离?

如何在数组中找到具有相同键值的对象?