[英]Using Object.keys with array of values
Given the following object给定以下对象
var obj = {'low': [1, 2, 3], 'medium': [4, 5, 6], 'high': [7, 8, 9, 10]}
is it possible, using Object.keys
(no loop), to get key (low/medium/high) by one of the corresponding values?是否有可能使用
Object.keys
(无循环)通过相应的值之一获取键(低/中/高)?
Maybe something like this:也许是这样的:
var obj = {'low': [1, 2, 3], 'medium': [4, 5, 6], 'high': [7, 8, 9, 10]};
function getKey(n) {
return Object.keys(obj).find(k => obj[k].includes(n));
}
If you would really like to avoid for loop
, you can use Array#reduce
instead.如果你真的想避免
for loop
,你可以使用Array#reduce
代替。
var obj = {'low': [1, 2, 3], 'medium': [4, 5, 6], 'high': [7, 8, 9, 10]}; const fn = (value, arr) => Object.entries(arr) .reduce((s, [key, a]) => (a.indexOf(value) > -1 ? key : s), null); console.log(fn(7, obj)); console.log(fn(1, obj));
You can do it with Object.keys , Object.values , Array#findIndex and Array#some .您可以使用Object.keys 、 Object.values 、 Array#findIndex和Array#some 来实现。
var obj = {'low': [1, 2, 3], 'medium': [4, 5, 6], 'high': [7, 8, 9, 10]}; function mySearch(obj,search) { return Object.keys(obj)[(Object.values(obj).findIndex(el => el.some(val => val===search)))]; } console.log(mySearch(obj, 5)); console.log(mySearch(obj, 8));
You can define a function to return the key according to the value passed.您可以定义一个函数来根据传递的值返回键。 the function uses Object.keys and Array.find()
该函数使用 Object.keys 和 Array.find()
var obj = {'low': [1, 2, 3], 'medium': [4, 5, 6], 'high': [7, 8, 9, 10]};
const findKeyByValue = (value)=>{
return Object.keys(obj).find(key => obj[key].find(element => element === value))
}
console.log(findKeyByValue(8))
It's not possible to do this without a custom approach, there are a lot of ways for accomplishing what you want.没有自定义方法就不可能做到这一点,有很多方法可以完成您想要的。
This approach uses the Proxy
object to find the key by the accessed property and keeps the entries to be able to provide quick access.这种方法使用
Proxy
对象通过访问的属性查找键,并保留条目以便能够提供快速访问。
const obj = {'low': [1, 2, 3], 'medium': [4, 5, 6], 'high': [7, 8, 9, 10]}, decorate = (o) => { const entries = Object.entries(o); return new Proxy(o, { get(_, accessedProperty) { let [key] = (entries.find(([_, values]) => values.includes(+accessedProperty)) || []); return key; } }); }, decoratedObj = decorate(obj); console.log(decoratedObj[1]); console.log(decoratedObj[4]); console.log(decoratedObj[10]); console.log(decoratedObj[11]);
You're looking for a different data structure than a basic object.您正在寻找与基本对象不同的数据结构。 Have a look at the bidirectional map .
看看双向映射。 This is a data strucure that allows you to look up values by their key, and keys by their value.
这是一种数据结构,允许您按键查找值,按值查找键。
I can recommend this implementation .我可以推荐这个实现。 It's part of mnemonist , which has a bunch of handy data structures for different use cases!
它是mnemonist的一部分,它为不同的用例提供了一堆方便的数据结构!
import BiMap from 'mnemonist/bi-map';
const obj = {'low': [1, 2, 3], 'medium': [4, 5, 6], 'high': [7, 8, 9, 10]};
const lookupTable = BiMap.from(obj);
console.log(lookupTable.get('low'))
// > [1,2,3]
console.log(lookupTable.inverse.get([1,2,3]))
// > 'low'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.