【问题标题】:How to check if all objects of array are included another array?如何检查数组的所有对象是否包含在另一个数组中?
【发布时间】:2019-06-05 01:34:30
【问题描述】:

我正在尝试检查对象array A 是否包含来自Bobjects

let A = [
    { name: "Max" },
    { name: "Jhon" },
    { name: "Naton" },
]

let B = [
    { name: "Max" },
    { name: "Naton" },
]

所以B 有两个objectsarray A 中。如何检查这个?

我正在尝试通过includes 来实现它:

  for(let entry of this.b){
      if(this.a.includes(entry)){
        console.log('includes');
      }
    }

但我在includes 上得到false

【问题讨论】:

  • 没错,您拥有所有具有相同属性值的不同对象。
  • 你的预期输出是什么?
  • 我希望我可以检查数组 A 是否包含数组的 B 对象。因此,对于两个数组中的每个相似对象,输出将是 console.log('includes')。
  • 你必须检查属性的值
  • 如何检查所有值是否相同?

标签: javascript arrays angular typescript object


【解决方案1】:

Array.includes() 方法将数组的条目与给定的值进行比较。因为您的数组条目是对象,所以它不会匹配。您必须自己循环数组并进行比较。

Array.some() 在数组上循环,如果您至少返回一个 true,则返回 true。当您想要验证某些内容时,此方法很有用。在我们的示例中,我们要验证数组 a 是否包含 b 条目。

const a = [{
    name: 'Max',
  },
  {
    name: 'Jhon',
  },
  {
    name: 'Naton',
  },
];

const b = [{
    name: 'Max',
  },
  {
    name: 'Naton',
  },
  {
    name: 'Daddy',
  },
];

console.log(b.map(x => a.some(y => y.name === x.name)));

如果我把它分解:

const a = [{
    name: 'Max',
  },
  {
    name: 'Jhon',
  },
  {
    name: 'Naton',
  },
];

const b = [{
    name: 'Max',
  },
  {
    name: 'Naton',
  },
  {
    name: 'Daddy',
  },
];

// Loop on every entry of the b array
b.forEach((x) => {
  // x here represent one entry
  // first it will worth { name: 'Max' }, then { name: 'Naton' } ...
  // for each value we are going to look at a if we can find a match
  const isThereAMatch = a.some((y) => {
    // y here is worth one entry of the a array
    if (y.name === x.name) return true;

    return false;
  });

  if (isThereAMatch === true) {
    console.log(`We have found ${x.name} in a`);
  } else {
    console.log(`We have not found ${x.name} in a`);
  }
});

【讨论】:

    【解决方案2】:

    你必须使用另一个循环,然后检查属性名称:

    var a = [
      {name: "Max"},
      {name: "Jhon"},
      {name: "Naton"},
    ];
    var b = [
      {name: "Max"},
      {name: "Naton"},
    ];
    
    for(let entry of b){
      for(let entry2 of a){
        if(entry2.name == entry.name){
          console.log('includes', entry.name);
        }
      }
    }

    或:您可以使用字符串版本的对象来检查includes()

    var a = [
      {name: "Max"},
      {name: "Jhon"},
      {name: "Naton"},
    ];
    var b = [
      {name: "Max"},
      {name: "Naton"},
    ];
    var aTemp = a.map(i => JSON.stringify(i));
    var bTemp = b.map(i => JSON.stringify(i));
    for(let entry of bTemp){
      if(aTemp.includes(entry)){
        console.log('includes', entry);
      }
    }

    【讨论】:

      【解决方案3】:

      当您使用 Array#includes() method 时,它将始终返回 false 因为它比较 objects 不相等,因为它们没有引用相同的 @ 987654326@.

      您应该比较对象 properties 而不是整个对象,您可以使用 Array#some() method 来做到这一点,如下所示:

      for (let entry of this.b) {
        if (this.b.some(x => x.name === entry.name)) {
          console.log('includes');
        }
      }
      

      演示:

      A = [{
          name: "Max"
        },
        {
          name: "Jhon"
        },
        {
          name: "Naton"
        },
      ]
      
      B = [{
          name: "Max"
        },
        {
          name: "Naton"
        },
      ]
      
      //Filter objects that exists in both arrays
      let result = A.filter(el=> B.some(x => x.name === el.name));
      console.log(result);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-24
        • 1970-01-01
        • 2021-12-26
        • 2021-06-04
        • 1970-01-01
        • 1970-01-01
        • 2019-05-05
        相关资源
        最近更新 更多