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
.
- 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
3 Answers
Reset to default 4The 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);