javascript - 如何在具有 1 个对象的数组上使用 .some() 方法?
问题描述
例如,我有这个对象数组:
const heights = [{tom: 160, jack:180, alex: 186}]
如何.some()
在数组内的对象上使用方法?
预期的:
if (heights.some((el) => el > 185)) {
console.log('Some of them are taller than 185')
}
//Output: Some of them are taller than 185
解决方案
如果数组确实只有一个对象,则仅提取该对象并取其值进行比较:
const heights = [{tom: 160, jack:180, alex: 186}]
if (Object.values(heights[0]).some((el) => el > 185)) {
console.log('Some of them are taller than 185')
}
推荐阅读
- database - Sqlite 获取日期范围的数据库值
- javascript - 如何重定向文件夹下的任何 url,保留子文件夹?
- python-3.x - 将日期时间四舍五入到最近小时的问题
- java - 我需要 JRE 来运行 Java 桌面应用程序吗?
- windows - 即使在正确的路径中,八度也找不到文件
- c++ - 无法打印存储在唯一指针中的地址
- python - 自定义 np.ndarray 的副本编辑原始
- javascript - 添加css时媒体查询不起作用
- vb.net - 如何获得一系列值 - Visual basic
- php - laravel 复选框不会更新到数据库 laravel