0👍
First of all are you sure you want categories to be arrays of arrays? If you want items of categories to be objects instead of arrays than you are using the wrong notation. If you want your category to be consisted of objects and not arrays, change your code to this:
category_1: [
{ id: 1, name: "Product 1", tags: [] },
{ id: 2, name: "Product 2", tags: [] },
],
category_2: [
{ id: 3, name: "Product 3", tags: [] },
{ id: 4, name: "Product 4", tags: ["blue"] },
{ id: 5, name: "Product 5", tags: [] },
{ id: 6, name: "Product 6", tags: ["blue"] },
],
With that out of the way, you can do what you want using filter function.
Your code should look like this:
getCategories(categoryName, wantedTag){
return categories[categoryName].filter(item => item.tags.includes(wantedTag));
}
- [Vuejs]-Can't copy all the styles from the original document to implement window.print(). Localhost works fine, prod not
- [Vuejs]-How to populate select options from json Array with VueJs
Source:stackexchange.com