I basically want to filter an object w/ a nested array in reference with another array of objects. The filtering needs to find all matching objects of the primary array with an AND logical operator (think Intersection). Below is a basic set up that should give you a clear idea of what I want to do. My current solution is not an 'intersection" solution I am trying to achieve. The some operator is giving me a UNION solution, instead of the INTERSECTION (OR vs AND).
interface Class1{
tags: Tag[];
}
interface Tag {
name: string;
}
sampleClassArray: Class1[] = [{
tags: [
{
name: 'Chris'
},
{
name: 'Eric'
},
{
name: 'Terry'
}
]
},
{
tags: [
{
name: 'Paul'
},
{
name: 'Brittney'
},
{
name: 'Brian'
}
]
},
{
tags: [
{
name: 'Eric'
},
{
name: 'Lee'
},
{
name: 'Polio'
}
]
}
}
sampleTagArray: Tag[] = [
{
name: 'Eric'
},
{
name: 'Bogey'
},
{
name: 'Samantha'
}
]
ngOnInit() {
let returnFilterResults = [];
returnFilterResults = this.sampleObject1.filter((sampleObject: sampleClassArray) =>
this.sampleTagArray.every((sampleTag: Tag) => sampleObject.tags.some((sampleObjectTag) => sampleTag.name === sampleObjectTag.name))
);
}