最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if all objects of array are included another array? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jan 10, 2019 at 8:39 cнŝdk 32.2k7 gold badges60 silver badges80 bronze badges asked Jan 10, 2019 at 8:11 DevlaDevla 3565 silver badges20 bronze badges 7
  • 2 that'r right, you have all different objects with some same property values. – Nina Scholz Commented Jan 10, 2019 at 8:12
  • What is your expected output? – holydragon Commented Jan 10, 2019 at 8:14
  • I want that I could check if array A contains array's B objects. So the output would be console.log('includes') for each similar object in both arrays. – Devla Commented Jan 10, 2019 at 8:15
  • you have to check the values of the property – Achraf Commented Jan 10, 2019 at 8:15
  • How to check if all values are the same ? – Devla Commented Jan 10, 2019 at 8:16
 |  Show 2 more ments

3 Answers 3

Reset to default 4

The method Array.includes() pare 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 parison.

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

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

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

You should pare 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);

发布评论

评论列表(0)

  1. 暂无评论