0

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))
        );
}
chris
  • 1
  • 1
  • if I got it right, you just need to replace `sampleTagArray.every` with `sampleTagArray.some` – Andrei Jun 30 '21 at 20:48
  • Does this answer your question? [Difference and intersection of two arrays containing objects](https://stackoverflow.com/questions/33356504/difference-and-intersection-of-two-arrays-containing-objects) – Heretic Monkey Jun 30 '21 at 20:51

0 Answers0