javascript - Find the key in an object which contains a particular value in an array
问题描述
I need to find the key(type:array) which contains a specific string. The actual data would look like as provided below
I tried to inverse the particular object with _.invert(). But doesnt make sense as it would convert the entire array to a string
{
"key1": ['asd','yrt','uyt'],
"key2": ['nbm','inb','oiu']
}
The response desired is that with we can fetch the key_name if an array element is provided. ie if the input is lets 'asd' we need to be able to say that the key was key1
解决方案
拿钥匙并找到包含的元素。
const
find = (object, value) => Object.keys(object).find(k => object[k].includes(value)),
object = { key1: ['asd','yrt','uyt'], key2: ['nbm','inb','oiu'] },
key = find(object, 'asd');
console.log(key);
推荐阅读
- laravel - 如何连接 laravel api 和颤振使用推送器制作实时聊天应用程序?
- javascript - 在 fullcalendar 中将选择添加到 columnHeaderHtml
- django - django:记录原始 sql 和其他输出
- laravel - 获取另一个表的值并求和
- git - 如何确保提交包含共享 git 存储库中的特定用户信息而不是组电子邮件 ID?
- java - 为什么它在摆动组件上显示错误
- html - 仅当它在视口中可见时才触发 css 动画
- google-cloud-platform - 如何让 GCP 启动脚本启动多个进程?
- python - 按 Python 分组并查找连续订阅计划
- core-graphics - iOS Core Graphics - 如何渲染 UIImage 以使用 16 位组件而不是 8 位来采样像素颜色?