[Vuejs]-Computed function ignores if else statement

0👍

return exits the function straight away, it has no possibility of executing anything after the return.

So, for your code… your if elses return inside each of their clauses, and your return statement returns. Nothing happens after the returns in your code.

So if you haven’t really finished doing all the things you want to do, don’t return, perhaps store the results in a variable, do the rest of what you need to do, then return the variable.

I’m not 100% of what you are intending, but it might look something like the following…

filteredProducts: function() {

    let filtered = this.products.filter((product) => {
        return product.productName.toLowerCase().includes(this.keyword.toLowerCase());
    });

    var vm = this;
    var category = vm.selectedCategory;

    if(category.includes("All")) {
        return filtered;
    } else {
        return filtered.filter((product) => {
            var keys = Object.keys(product);
            var matchFilter = false;
            category.forEach((key) => {
                if(product[key] === true) {
                    matchFilter = true;
                }
            });
            return matchFilter;
        });             
    }
},

Leave a comment