Answer the question
In order to leave comments, you need to log in
Vue js how to make a list filter if an element has multiple categories in an array?
Hello, there is a small codepen of mine for training here:
https://codepen.io/msdoc11/pen/ZEJverK
If you specify a size for the product like this: size: "XL", that is, one parameter, then everything works.
If you specify an array size: ["XL", "XXL"], then it does not work. How to properly compare two arrays?
Now the code contains an array of data, so the size filter does not work.
Answer the question
In order to leave comments, you need to log in
computed: {
filteredProducts() {
const keyword = this.keyword.toLowerCase();
return this.products.filter(item => (
(item.name.toLowerCase().includes(keyword)) &&
(!this.colors.length || this.colors.includes(item.color)) &&
(!this.sizes.length || this.sizes.some(n => item.size.includes(n))) &&
(!this.categories.length || this.categories.includes(item.category))
));
},
...
computed: {
colorFilter() {
return Array
.from(new Set(this.products.map(n => n.color)))
.sort((a, b) => a.localeCompare(b));
},
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question