Building an E-Commerce Store - Browsing Products Chapter 10
Although, feel free to use this if you are more comfortable with it.
Duplicate the code to work with tags, however as tags are an array themselves, we need
to loop through each tag and add accordingly:
for(let product of category.productDetails) {
if(product.hasOwnProperty('vendor')) {
let vendor = product.vendor;
if(vendor.handle) {
if(!vendor.handle.count.includes(product.handle)) {
category.values[item.handle].count.push(product.handle);
}
} else {
vendors.values[vendor.handle] = {
...vendor,
count: [product.handle]
}
}
}
if(product.hasOwnProperty('tags')) {
for(let tag of product.tags) {
if(tag.handle) {
if(topicTags.values[tag.handle]) {
if(!topicTags.values[tag.handle].count.includes(product.handle))
{
topicTags.values[tag.handle].count.push(product.handle);
}
} else {
topicTags.values[tag.handle] = {
...tag,
count: [product.handle]
} } } } } }
Our code is already getting repetitive and complex, let's simplify it by creating a method to
handle the repetitive code.