gpt4 book ai didi

javascript - 如何从数组中返回具有最高键值和名称的对象?

转载 作者:行者123 更新时间:2023-11-28 14:11:08 25 4
gpt4 key购买 nike

当我试图获取一个包含具有最高值的所有名称的对象时,我只获取一个值和名称,而不是具有相同最高值的所有现有名称?//{ name: 'Stephen', 总数: 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)

关于javascript - 如何从数组中返回具有最高键值和名称的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59395160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com