简体   繁体   中英

How to check if all objects of array are included another array?

I am trying to check if object array A includes objects from B .

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

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

So B has two objects that is in array A . How to check this ?

I am trying to achieve it with includes :

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

But I get false on includes .

You have to use another loop, then check the property name:

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

OR: You can use string version of object to check with 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); } } 

The method Array.includes() compare the entries of the array with the given value. Because your array entries are objects, it will not match. You have to loop at the array yourself and make the comparison.

Array.some() loops on an array and returns true if you returns true at least one. This method is useful when you want to verify something. In our example, we want to verify if the array a contains the b entry.

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


If I break it down :

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

When you use Array#includes() method it will always return false because it's comparing objects which aren't equal because they aren't referencing the same object .

You should compare objects properties and not whole objects, you can do it using Array#some() method like this:

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

Demo:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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