[SOLVED] How to filter an array by an array in typescript and return result

Issue

I am able to print the result, how would I return the result here to be assigned to an object

Attribute text is a string that we must split

this.childArrayAll.filter(x => x.attributeText1.split(",").forEach(y => {
    if(this.searchFilterForm.controls['types'].value.includes(y) )
        console.log(x);
}))

Solution

You can use Array’s some method to do it in clean and optimized way.

this.childArrayAll.filter(x => {
    const attributes = x.attributeText1.split(",");

    // Check if there is a match
    return attributes.some(attribute => this.searchFilterForm.controls['types'].value.includes(attribute));
});

Answered By – dhaker

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *