2016-12-13 6 views

答えて

2

JavaScriptを使用Array#reduce方法。

var data = { 
 
    players: { 
 
    p1: { 
 
     name: 'joe', 
 
     points: 25 
 
    }, 
 
    p2: { 
 
     name: ' frank', 
 
     points: 35 
 
    }, 
 
    p3: { 
 
     name: 'tim', 
 
     points: 55 
 
    } 
 
    } 
 
}; 
 

 
var res = data.players[ 
 
    // get all property names 
 
    Object.keys(data.players) 
 
    // get the property name which holds the hghest point 
 
    .reduce(function(a, b) { 
 
    // compare and get the property which holds the highest 
 
    return data.players[a].points < data.players[b].points ? b : a; 
 
    }) 
 
]; 
 

 
console.log(res);

0
const data = {p1: {name: 'hello', points: 1}, p2: {name: 'world', points: 2}} 
const dataList = _.values(data) 
const maxPointItem = dataList.reduce((prev, curr) => prev.points < curr.points ? curr : prev), dataList[0]) 
2

使用_.maxBy

_.chain(players) 
    .values() 
    .maxBy('points') 
    .value(); 
関連する問題