javascript - 我们如何获得 JSON 数组中的最大数字?
问题描述
我想计算最大数量的“probs”,并使用 JavaScript 或 jQuery 获得最大数量的“标签”。
例如,在下面,“郁金香”。
我会很感激你的帮助。
result_predictions=[
{
"label": "roses",
"prob": 0.0056262025609612465
},
{
"label": "daisy",
"prob": 0.005660845898091793
},
{
"label": "dandelion",
"prob": 0.005297524854540825
},
{
"label": "tulips", // What I want to pick
"prob": 0.9730507731437683 //the highest number
},
{
"label": "sunflowers",
"prob": 0.010364603251218796
}
]
解决方案
const result_predictions =
[ { label: "roses", prob: 0.0056262025609612465 }
, { label: "daisy", prob: 0.005660845898091793 }
, { label: "dandelion", prob: 0.005297524854540825 }
, { label: "tulips", prob: 0.9730507731437683 }
, { label: "sunflowers", prob: 0.010364603251218796 }
]
const label_ofMax = result_predictions
.reduce((a,c)=>(a.prob<c.prob)?c:a)
.label
console.log( label_ofMax, )
推荐阅读
- salesforce - 使用赛普拉斯测试自动化,用于使用 Salesforce 构建的网站
- python - 当旧线程死亡时重新启动函数
- python - 对于大小为 1 的轴 1,索引 6 超出范围
- google-sheets - 谷歌表格:在一行中查找最后一个非空单元格并从不同行中提取单元格值
- html - 如何根据其宽度制作表格单元格内容文本?
- reactjs - 我正在尝试验证我的电子邮件,如果输入我可以验证电子邮件,但如果粘贴电子邮件,我的电子邮件验证失败
- java - 如何在 Flutter 中显示默认锁定屏幕而不是我的启动器应用程序?
- ios - Readium 库第二页单词 hi-lighter 在 ios 的横向中不起作用
- excel-formula - Coda 文档如何通过匹配 id 来获取值
- c# - 删除 Excel 中的组合框并尝试使用 VSTO 插件保存工作簿时 Excel 崩溃