javascript - 如何从具有最高键值的数组中返回对象以及名称?
问题描述
当我试图获得一个所有名称都具有最高值的对象时,我只得到一个值和名称,而不是所有现有名称都具有相同的最高值?// { 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 }
解决方案
利用
const result = students.filter(student => student.total == max)
推荐阅读
- npm - 学习webpack配置webpack-dev-server时报如下错误。我遵循了官方的 webpack 文档
- javascript - 如何正确创建登录用户的 json 响应以进行反应
- javascript - 我对 Cloud Firestore V9 和使用 React Js 有疑问。在我的一个文档中,有一张地图。如何访问此地图?
- performance - 核心网络生命体征 - 移动 - CLS 问题:超过 0.25(移动)
- td-engine - 如何将TDengine数据库中2020-06-02 10:40:28.001的时间类型转换为1591065628?
- flutter - 流生成器 ConnectionState.active 多次调用并在颤动中一次又一次地返回小部件
- javascript - 如何为暗/亮模式切换保存 cookie?W3 学校代码
- python - 将多个重复的模式名称分配给列表值
- python - 如何将导出的文件保存到与py文件相同的路径并将我的csv文件重命名为文件夹名称
- javascript - 在将浏览器窗口大小调整为全屏时,将网格项目调整为之前的状态