javascript - How to verify a key exists in an array
问题描述
I have this variable:
var teams = [{manutd: true, barcelona: true, real: false}];
Given a string, I want to know if there is an entry in team's entries. So If I have:
var team = "real"
I want to query the "teams" and get true as "real" is one of the keys in the array. I have tried with includes but it fails. Maybe because the keys are not strings?
解决方案
Using Object.keys
, you can get the keys of the object item in an array.
var teams = [{manutd: true, barcelona: true, real: false}];
var team = 'real';
const isExisted = teams.some((item) => Object.keys(item).includes(team));
console.log(isExisted);
推荐阅读
- java - 我的刚体模型从地板上掉下来
- c++ - 如何从 C++ 中的格式化字符串派生整数?
- ionic-framework - 离子背景地理定位停止工作
- android - 模拟“su root”命令来修复 Permission Denied 错误
- ios - 如何使用 Swift 将数据从 IOS 应用程序发布到 MySQL 表?
- ios - 运行cordova run ios时出现“服务无效”
- android - 如何在 viewpager 中暂停视频播放器
- ios - BGTaskScheduler 失败,不存在具有标识符的后台任务,或者它可能已经结束
- javascript - Infinite Scroll React 组件仅在向下滚动时加载一个新的对象切片然后停止
- python - 在 PyTorch 中为简单的神经网络添加训练和测试精度