Building an E-Commerce Store - Browsing Products Chapter 10
Create a methods object with a function of addTopic. This will take two parameters: the
object to append to and the singular item. For example, its usage would be:
if(product.hasOwnProperty('vendor')) {
this.addTopic(this.topics.vendor, product.vendor, product.handle);
}
Create the function and abstract out the logic from inside the hasOwnProperty if
declaration. Name the two parameters category and item, and update the code
accordingly:
methods: {
addTopic(category, item, handle) {
if(item.handle) {
if(category.values[item.handle]) {
if(!category.values[item.handle].count.includes(handle)) {
category.values[item.handle].count.push(handle);
}
} else {
category.values[item.handle] = {
...item,
count: [handle]
}
}
}
}
}
Update the filters computed function to use the new addTopic method. Remove the
variable declarations at the top of the function, as they are being passed directly into the
method:
filters() {
if(this.categoriesExist) {
let category = this.categoryProducts(this.slug);
for(let product of category.productDetails) {
if(product.hasOwnProperty('vendor')) {
this.addTopic(this.topics.vendor, product.vendor, product.handle);
}
if(product.hasOwnProperty('tags')) {