有没有办法将键传递给 JavaScript 数组,其中字符串可能匹配对象的多个值?
Is there a way to pass keys into a JavaScript array where a a string might match multiple values of an object?
所以,如果我只有一个键可以匹配,那么像这样:
var str = "foo";
let [key,val] = Object.entries(obj).find(([key,val]) => val== str);
return key;
会很漂亮。但是如果值匹配,有没有办法添加多个键?
举例说明可能匹配上述字符串的对象:
obj = {
quay: "foo",
key1: "blargh",
yaya: "foo",
idnet: "blat",
blah: "foo",
hahas: "blargh"
}
我想要做的是 return 所有基于匹配变量 [=] 的“foo”键(quay
、yaya
和 blah
) 16=]从上面。
我确定答案很简单,但我忽略了。
使用 filter
而不是 find
和 map
来删除值。
const obj = { quay: "foo", key1: "blargh", yaya: "foo", idnet: "blat", blah: "foo", hahas: "blargh" }
const str = 'foo';
const keys = Object.entries(obj).filter(([, val]) => val === str).map(([key]) => key);
console.log(keys);
使用Object.keys()
获取键数组,并使用Array.filter()
过滤它们。在过滤器的谓词函数中,使用键从对象中获取相关值 - obj[key]
并将其与 str
.
进行比较
const str = 'foo'
const obj = {"quay":"foo","key1":"blargh","yaya":"foo","idnet":"blat","blah":"foo","hahas":"blargh"}
const keys = Object.keys(obj).filter(key => obj[key] === str)
console.log(keys)
您可以像现在一样获取所有条目,但不是使用查找,而是过滤您要查找的具有 str
值的所有条目并将其映射到列表。像这样:
const str = "foo";
const obj = {
quay: "foo",
key1: "blargh",
yaya: "foo",
idnet: "blat",
blah: "foo",
hahas: "blargh"
}
const repeated = Object.entries(obj)
.filter(([key, val]) => val === str)
.map(([key, val]) => key);
console.log(repeated);
另一种方式:
const obj = {
quay: "foo",
key1: "blargh",
yaya: "foo",
idnet: "blat",
blah: "foo",
hahas: "blargh"
}
let items = Object.fromEntries(Object.entries(obj).filter(([key, val]) => val === 'foo'));
console.log(Object.keys(items))
所以,如果我只有一个键可以匹配,那么像这样:
var str = "foo";
let [key,val] = Object.entries(obj).find(([key,val]) => val== str);
return key;
会很漂亮。但是如果值匹配,有没有办法添加多个键?
举例说明可能匹配上述字符串的对象:
obj = {
quay: "foo",
key1: "blargh",
yaya: "foo",
idnet: "blat",
blah: "foo",
hahas: "blargh"
}
我想要做的是 return 所有基于匹配变量 [=] 的“foo”键(quay
、yaya
和 blah
) 16=]从上面。
我确定答案很简单,但我忽略了。
使用 filter
而不是 find
和 map
来删除值。
const obj = { quay: "foo", key1: "blargh", yaya: "foo", idnet: "blat", blah: "foo", hahas: "blargh" }
const str = 'foo';
const keys = Object.entries(obj).filter(([, val]) => val === str).map(([key]) => key);
console.log(keys);
使用Object.keys()
获取键数组,并使用Array.filter()
过滤它们。在过滤器的谓词函数中,使用键从对象中获取相关值 - obj[key]
并将其与 str
.
const str = 'foo'
const obj = {"quay":"foo","key1":"blargh","yaya":"foo","idnet":"blat","blah":"foo","hahas":"blargh"}
const keys = Object.keys(obj).filter(key => obj[key] === str)
console.log(keys)
您可以像现在一样获取所有条目,但不是使用查找,而是过滤您要查找的具有 str
值的所有条目并将其映射到列表。像这样:
const str = "foo";
const obj = {
quay: "foo",
key1: "blargh",
yaya: "foo",
idnet: "blat",
blah: "foo",
hahas: "blargh"
}
const repeated = Object.entries(obj)
.filter(([key, val]) => val === str)
.map(([key, val]) => key);
console.log(repeated);
另一种方式:
const obj = {
quay: "foo",
key1: "blargh",
yaya: "foo",
idnet: "blat",
blah: "foo",
hahas: "blargh"
}
let items = Object.fromEntries(Object.entries(obj).filter(([key, val]) => val === 'foo'));
console.log(Object.keys(items))