简体   繁体   English

在Javascript中比较一个对象的键和另一个对象的值

[英]Comparing an object's key with another object's value in Javascript

I would like to compare an object's key with another object's value.我想将一个对象的键与另一个对象的值进行比较。 This function should return the object(of the key) if they are the same.如果它们相同,此函数应返回(键的)对象。

const obj1 = {
"list": [
{
  sequence: {
    id: '6543'
  }
},
{
  sequence: {
    id: '5667'
  }
},
{
  sequence: {
    id: '7899'
  }
}
]}

const obj2 = {
      "id": "alias",
      "value": {
        "6543": "Htayu",
        "1456": "dfwd"
      }
    }

Now I would like to compare obj1.sequence.id with obj2.value.key.现在我想比较 obj1.sequence.id 和 obj2.value.key。 I want to compare each object of obj1 with each key of obj2.我想将 obj1 的每个对象与 obj2 的每个键进行比较。 If they are same then I need obj2 matched key value pair.如果它们相同,那么我需要 obj2 匹配的键值对。

For example:例如:

obj1 6543 = obj2 6543
const requiredValue = [{"6543": "Htayu"}]

I already did this.我已经这样做了。

const customName = obj2.find((obj) => obj.id === 
 'alias');
const name = Object.keys(customName.value);

It is not working.它不工作。 Please help!请帮忙!

 const obj1 = { "list": [ { sequence: { id: '6543' } }, { sequence: { id: '5667' } }, { sequence: { id: '7899' } } ] } const obj2 = { "id": "alias", "value": { "6543": "Htayu", "1456": "dfwd" } } let data = Object.entries(obj2.value).filter(a => obj1.list.filter(x => x.sequence.id === a[0])).map(a => { return {[a[0]]: a[1]}; }); console.log(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM