首页 > 解决方案 > 如何从具有最高键值的数组中返回对象以及名称?

问题描述

当我试图获得一个所有名称都具有最高值的对象时,我只得到一个值和名称,而不是所有现有名称都具有相同的最高值?// { name: 'Stephen', total: 85 } 任何帮助将不胜感激。

const students = [
{ name: 'Andy', total: 40 },
{ name: 'Seric', total: 50 },
{ name: 'Stephen', total: 85 },
{ name: 'David', total: 30 },
{ name: 'Phil', total: 40 },
{ name: 'Eric', total: 85 },
{ name: 'Cameron', total: 30 },
{ name: 'Geoff', total: 30 }];

const max = Math.max(...students.map(e => e.total))

const result = students.find(student => student.total === max)

console.log(result)//{ name: 'Stephen', total: 85 } 

标签: javascript

解决方案


利用

const result = students.filter(student => student.total == max)

推荐阅读