简体   繁体   English

获取对象中属性的最大值

[英]Get max value of property in object

I am trying to get the max value of all the wins properties in an object. 我试图获取对象中所有wins属性的最大值。 Here is a shortened example of the object, of which I am trying to get the max value of the wins props, which would be 11. 这是该对象的简化示例,我试图获取该对象的最大值,即11。

var coach_wins = {
  "player1": [
    {
      year: 2015,
      wins: 6
    },
    {
      year: 2016,
      wins: 6
    }
  ],
  "player2": [
    {
      year: 2015,
      wins: 11
    },
    {
      year: 2016,
      wins: 6
    }
  ]
};

Currently I am counting the length of the object and then using a for loop to loop through the object and get the keys: 目前,我正在计算对象的长度,然后使用for循环遍历对象并获取键:

coach_wins[Object.keys(coach_wins)[i]];

then do this again and store the values of each wins value; 然后再次执行此操作并存储每个获胜值的值;

Is there a more efficient way? 有没有更有效的方法?

Thanks. 谢谢。

 var coach_wins = { "player1": [ { year: 2015, wins: 6 }, { year: 2016, wins: 6 } ], "player2": [ { year: 2015, wins: 11 }, { year: 2016, wins: 6 } ] }; var max = Object.values(coach_wins) //get all the values of the object .reduce(function(largest, player){ //reduce the max for all player return player.reduce(function(largest, record){ //reduce the max for each player return (largest > record.wins ? largest : record.wins); }, largest); }, 0); console.log(max); 

What about this? 那这个呢?

Object.values(coach_wins).reduce(
  (result, player) => Math.max(result, player.reduce(
    (wins, winsPerYear) => Math.max(wins, winsPerYear.wins)
  , 0))
, 0); // --> 11

If player's individual arrays will always containing 2 that's fine I guess but if it can be changed you can go to some thing like this. 如果玩家的单个数组始终包含2,那是可以的,但是如果可以更改,则可以执行以下操作。 Bit of code but won't put you in exceptions 一点点的代码,但不会让您陷入异常

function getMaxWins(){
    var maxWins = 0;
    for (var curPlayer in coach_wins) {

        //checking whether is it contains records
        var playerRecords = coach_wins[curPlayer];

        if(playerRecords){
            playerRecords.forEach(function (playerRecord_) {
                if(maxWins < playerRecord_.wins) maxWins = playerRecord_.wins;
            });
        }
    }
    return maxWins;
}

console.log(getMaxWins());

You can try this.. 你可以试试看

var max = coach_wins[Object.keys(coach_wins)[0]][0].wins;
for (var player in coach_wins ) {
if (coach_wins.hasOwnProperty(player)) {
   var current_max = Math.max.apply(Math,coach_wins[player].map(function(o){return o.wins;}));
    max = current_max > max ? current_max:max;

        }
    }

或者,使用功能性方法和ES6语法,您可以

Math.max(...[].concat(...Object.values(coach_wins).map(p=>p.map(g=>g.wins))))

You can define a variable set to 0 , use Object.values() to iterate values of object, .forEach() to iterate each array, if value of wins is greater than variable, set variable to value of "wins" property 您可以定义一个变量设置为0 ,使用Object.values()迭代对象的值, .forEach()迭代每个数组,如果wins的值大于变量,则将变量设置为"wins"属性的值

 var coach_wins = { "player1": [ { year: 2015, wins: 6 }, { year: 2016, wins: 6 } ], "player2": [ { year: 2015, wins: 11 }, { year: 2016, wins: 6 } ] }; let res; Object.values(coach_wins) .forEach(prop => prop.forEach(({wins}) => { if (!res || wins > res) res = wins }) ); console.log(res) 

An expected result object would have been appreciated but I am guessing this is what you are looking for: 预期的结果对象将不胜感激,但我猜这是您在寻找的东西:

var result = {};

for(var player in coach_wins){
      result[player] = coach_wins[player].reduce((max, value) => {return value.wins>max?value.wins:max}, 0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM