Tony S
Tony S

Reputation: 571

JavaScript filter by all values of an array

Here my code and what i tried :

filterPrestationsByServiceSelected(arrayOfServices) {
    console.log(arrayOfServices); // ['Repassage', 'Couture']
    this.filteredPrestationsByService = this.filteredPrestations.filter(item => item.service.name.includes(arrayOfServices.values()));
},

I want to filter all items of this.filteredPrestations where the service name contains values of the arrayOfServices.

Anyone have an idea of what i can do ? Thank's !

Upvotes: 1

Views: 58

Answers (3)

Sooraj m v
Sooraj m v

Reputation: 11

Can you try this code. I think this code will work.

filterPrestationsByServiceSelected(arrayOfServices) {
    console.log(arrayOfServices); // ['Repassage', 'Couture']
    this.filteredPrestationsByService = this.filteredPrestations.filter(item => arrayOfServices.includes(item.service.name));
},

Upvotes: 0

iwaduarte
iwaduarte

Reputation: 1699

You have to compare the items of a list with another. So you would have to have a compare each element of one data structure with another. Since you are comparing arrays you should do that way:

filterPrestationsByServiceSelected(arrayOfServices) {
    console.log(arrayOfServices); // ['Repassage', 'Couture']
    this.filteredPrestationsByService = this.filteredPrestations.filter(item => arrayOfServices.find(e => e === item.service.name))
},

That way you could compare the elements one by one.

Upvotes: 0

EugenSunic
EugenSunic

Reputation: 13713

Remove .values() it returns an iterator which you don't need

filterPrestationsByServiceSelected(arrayOfServices) {
    console.log(arrayOfServices); // ['Repassage', 'Couture']
    this.filteredPrestationsByService = this.filteredPrestations.filter(item => item.service.name.includes(arrayOfServices));
}

Upvotes: 1

Related Questions