Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取对象中属性的最大值_Javascript_Jquery_Json_Object - Fatal编程技术网

Javascript 获取对象中属性的最大值

Javascript 获取对象中属性的最大值,javascript,jquery,json,object,Javascript,Jquery,Json,Object,我正在尝试获取对象中所有wins属性的最大值。这是一个对象的简化示例,我正在尝试获取wins道具的最大值,即11 var coach_wins = { "player1": [ { year: 2015, wins: 6 }, { year: 2016, wins: 6 } ], "player2": [ { year: 2015, wins: 11 },

我正在尝试获取对象中所有wins属性的最大值。这是一个对象的简化示例,我正在尝试获取wins道具的最大值,即11

var coach_wins = {
  "player1": [
    {
      year: 2015,
      wins: 6
    },
    {
      year: 2016,
      wins: 6
    }
  ],
  "player2": [
    {
      year: 2015,
      wins: 11
    },
    {
      year: 2016,
      wins: 6
    }
  ]
};
目前,我正在计算对象的长度,然后使用for循环在对象中循环并获取关键点:

coach_wins[Object.keys(coach_wins)[i]];
然后再次执行此操作并存储每个wins值的值

有没有更有效的方法

谢谢。

这个怎么样

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

Object.values(coach_wins).reduce(
  (result, player) => Math.max(result, player.reduce(
    (wins, winsPerYear) => Math.max(wins, winsPerYear.wins)
  , 0))
, 0); // --> 11
var coach\u wins={
“玩家1”:[
{
年份:2015年,
胜:6
},
{
年份:2016年,
胜:6
}
],
“玩家2”:[
{
年份:2015年,
胜利:11
},
{
年份:2016年,
胜:6
}
]
};
var max=Object.values(coach_wins)//获取对象的所有值
.reduce(函数(最大,玩家){//减少所有玩家的最大值
return player.reduce(函数(最大值,记录){//减少每个播放器的最大值
返回(最大>记录.wins?最大:记录.wins);
},最大);
}, 0);
控制台日志(最大值)
var coach\u wins={
“玩家1”:[
{
年份:2015年,
胜:6
},
{
年份:2016年,
胜:6
}
],
“玩家2”:[
{
年份:2015年,
胜利:11
},
{
年份:2016年,
胜:6
}
]
};
var max=Object.values(coach_wins)//获取对象的所有值
.reduce(函数(最大,玩家){//减少所有玩家的最大值
return player.reduce(函数(最大值,记录){//减少每个播放器的最大值
返回(最大>记录.wins?最大:记录.wins);
},最大);
}, 0);

控制台日志(最大值)如果玩家的单个数组总是包含2,我想这很好,但如果可以更改,你可以这样做。一点代码,但不会使您出现异常

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());
函数getMaxWins(){ var-maxWins=0; for(教练中的球员获胜){ //检查它是否包含记录 var playerRecords=教练赢得[curPlayer]; 如果(播放记录){ playerRecords.forEach(函数(playerRecords){ 如果(maxWins
如果玩家的单个数组总是包含2,我想这很好,但如果可以更改,您可以使用类似的方法。一点代码,但不会使您出现异常

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());
函数getMaxWins(){ var-maxWins=0; for(教练中的球员获胜){ //检查它是否包含记录 var playerRecords=教练赢得[curPlayer]; 如果(播放记录){ playerRecords.forEach(函数(playerRecords){ 如果(maxWins
你可以试试这个

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;

        }
    }
你可以试试这个

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

或者,使用函数方法和ES6语法,您可以

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

您可以将变量集定义为
0
,使用
Object.values()
迭代对象的值,
.forEach()
迭代每个数组,如果
wins
的值大于variable,则将变量设置为
wins”
属性的值

var coach\u wins={
“玩家1”:[
{
年份:2015年,
胜:6
},
{
年份:2016年,
胜:6
}
],
“玩家2”:[
{
年份:2015年,
胜利:11
},
{
年份:2016年,
胜:6
}
]
};
让我们重新来过;
对象值(教练获胜)
.forEach(prop=>
prop.forEach(({wins})=>{
如果(!res | | wins>res)res=wins
})
);

console.log(res)
您可以定义一个设置为
0
的变量,使用
Object.values()
迭代对象的值,
.forEach()
迭代每个数组,如果
wins
的值大于variable,则将变量设置为
“wins”
属性的值

var coach\u wins={
“玩家1”:[
{
年份:2015年,
胜:6
},
{
年份:2016年,
胜:6
}
],
“玩家2”:[
{
年份:2015年,
胜利:11
},
{
年份:2016年,
胜:6
}
]
};
让我们重新来过;
对象值(教练获胜)
.forEach(prop=>
prop.forEach(({wins})=>{
如果(!res | | wins>res)res=wins
})
);

console.log(res)
一个预期的结果对象会很受欢迎,但我猜这就是您想要的:

var result = {};

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

一个预期的结果对象会很受欢迎,但我猜这就是你想要的:

var result = {};

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

你想要全部还是每个球员?在比较不同的玩家之前,是否应该对每个玩家的胜利进行汇总?@taplar我已经对问题进行了编辑,使其更加具体。您是希望对所有玩家进行汇总,还是对每个玩家进行汇总?在比较不同的玩家之前,是否应该对每个玩家的胜利进行汇总?@taplar我已经对这个问题进行了编辑,使之更加具体。