简体   繁体   中英

Get max value of property in object

I am trying to get the max value of all the wins properties in an object. 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.

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:

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. 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

 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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