有没有办法将密钥传递到aa字符串可能匹配对象的多个值的JavaScript数组中?

发布于 2025-01-26 13:11:48 字数 546 浏览 5 评论 0原文

因此,如果我只有一个钥匙可以匹配,那么类似的东西

    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"
    }

我要做的就是返回所有“ foo”键(quayyaya> Blah)基于上面的匹配var str

我确定答案是我忽略的简单的东西。

So, if I have only one key to match, then something like:

    var str = "foo";

    let [key,val] = Object.entries(obj).find(([key,val]) => val== str);
    return key;

would work beautifully. But is there a way to add multiple keys if the value matches?

To give example of an object that might match the above string:

    obj = {
      quay: "foo",
      key1: "blargh",
      yaya: "foo",
      idnet: "blat",
      blah: "foo",
      hahas: "blargh"
    }

What I want to do is return all of the "foo" keys (quay, yaya, and blah) based on the matching var str from above.

I'm sure the answer is something simple I'm overlooking.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风流物 2025-02-02 13:11:48

使用过滤器而不是查找地图以删除值。

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);

Use filter instead of find and map to remove the values.

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);

千紇 2025-02-02 13:11:48

使用object.keys()获取一系列键,然后用array.filter()过滤它们。在过滤器的谓词函数中,使用键 - obj [键]从对象中获取相关值,并将其与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)

Use Object.keys() to get an array of keys, and filter them with Array.filter(). In the filter's predicate function take the relevant value from the object using the key - obj[key] and compare it to 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)

囍笑 2025-02-02 13:11:48

您可以像做所有的条目一样获取所有条目,但是不要使用查找,而是用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);

You could get all the entries like you are doing, but instead of using find, filtering all the entries with the value on str that you want to find and mapping it to a list. Like this:

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);

挽清梦 2025-02-02 13:11:48

另一种方法:

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))

Another way:

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))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文