【发布时间】:2021-06-26 12:27:21
【问题描述】:
例如我有这个数组:
const ageArr = [{tom: 24}, {jack: 20}, {anna: 30}]
如何在不指定名称的情况下使用.some() 搜索所有值?
预期:
if (ageArr.some((el) => el > 19)) {
console.log('Some of them are older than 19')
}
//Output: People are older than 19
【问题讨论】:
-
"...不指定名称?" - 使用更合适的对象/结构怎么样:
{ name: "Tom", age: 24 }- 这样会更容易... -
我同意@Andreas。使用 Object.keys/values 提出的(当前)解决方案很麻烦,如果您的数据发生更改(例如,如果对象获得另一个属性),则很容易中断。使用合适的数据格式会更简洁,更不容易出错
标签: javascript