我有以下数组的对象,我想找到任何一年中任何人的最高权重(重量足够,我不需要相应的年份或名称)。
提前致谢:)
var arr = [
{
'name': 'Bob',
'weights': [
{
'weight': 90,
'year': 2018
},
{
'weight': 85,
'year': 2017
},
// etc.
]
},
'name': 'Charlie',
'weights': [
{
'weight': 65,
'year': 2018
},
{
'weight': 60,
'year': 2017
},
// etc.
]
},
// etc.
]
答案 0 :(得分:3)
您可以使用Math.max
和扩展语法来查找最大值,但您还需要使用map
来首先在一个数组中获取权重值。
var arr = [{"name":"Bob","weights":[{"weight":90,"year":2018},{"weight":85,"year":2017}]},{"name":"Charlie","weights":[{"weight":65,"year":2018},{"weight":60,"year":2017}]}]
var max = Math.max(...[].concat(...arr.map(({weights}) => weights.map(({weight}) => weight))))
console.log(max)