首页 > 解决方案 > 如何在具有 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

标签: javascript

解决方案


如果数组确实只有一个对象,则仅提取该对象并取其值进行比较:

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')
}


推荐阅读