javascript - Javascript函数返回数组对象阈值之间的值
问题描述
我scores
在 JavaScript 中有以下对象:
[
{
"id":37,
"title":"Over achieving",
"description":"Exceeding expectations",
"threshold":10,
},
{
"id":36,
"title":"Achieving",
"description":"Achieving expectations",
"threshold":6,
},
{
"id":35,
"title":"Under achieving",
"description":"Not achieving expectations",
"threshold":3,
}
]
我试图弄清楚如何创建一个方法,该方法将根据分数阈值确定的值返回分数对象。
我尝试了以下方法,但它仅在值等于分数阈值时才返回分数,而不是在它之间。
scores.find(o => o.threshold <= progress && o.threshold >= progress)
所以场景是,一个人的进度value
为 5,我想要返回id
35 的分数数组项的方法,因为 5 介于 3 和 6 之间。同样,如果进度value
是 7,那么我想要方法返回带有id
36 的分数数组项,因为 7 介于 6 和 10 之间。
我确定我离我不远了。
解决方案
您似乎正在寻找数组中阈值低于或等于 progress的第一项。表达方式
scores.find(o => o.threshold <= progress)
会这样做。
推荐阅读
- objective-c - 从 PHAsset(或 assets-library://)获取 UIImage
- javascript - 我如何 e2e 测试 axios
- python - 如何在没有覆盖重定向或属性的情况下在 linux LXDE 上使用 tkinter 删除标题栏?
- gradle - Gatling 报告场景中第一个请求组的响应时间更长
- sql-server - 创建 2 对唯一 id 数据行
- javascript - 在 jquery 中调整视口大小时禁用和启用滚动窗口
- java - 如何正确地只允许业务部分分类帐中的数量为 5
- java - Libgdx 断言失败 [表达式:pointCount > 0]
- javascript - 如何从另一个页面获取值并将其输出到选择列表?JAVASCRIPT
- mysql - 更换电脑后我的数据库变成了“只读”