簡體   English   中英

JavaScript 按數組的所有值過濾

[英]JavaScript filter by all values of an array

這是我的代碼和我嘗試過的:

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

我想過濾this.filteredPrestations所有項目,其中service name包含arrayOfServices值。

任何人都知道我能做什么? 謝謝 !

刪除 .values() 它返回一個你不需要的迭代器

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

你可以試試這個代碼。 我認為這段代碼會起作用。

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

您必須將列表中的項目與另一個進行比較。 因此,您必須將一個數據結構的每個元素與另一個進行比較。 由於您正在比較數組,因此您應該這樣做:

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

這樣你就可以一一比較元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM