سؤال

I'm trying to generate a breadcrumb from a tree structure in javascript like this

category {
  Description: string;
  Categories: category[];
}

for that I made the following function wich recursively filters branches except for the one containing the leaf that meets the condition.

                function recursiveFilter(category) {
                    if (category.Description == CURRENT_CATEGORY_DESCRIPTION) {
                        return true;
                    }
                    else {
                        category.Categories = category.Categories.filter(function (subcategory) { recursiveFilter(subcategory); });
                        return subcategory.Categories.length > 0;
                    }
                }

this is currently returning no categories even if while debugging I checked that the condition is met.

I know there's something silly I'm missing. but I need other eyes to point it out.

هل كانت مفيدة؟

المحلول

Finally got it as I was explaining it to a colleague, I missed a return

            function recursiveFilter(category) {
                if (category.Description == CURRENT_CATEGORY_DESCRIPTION) {
                    return true;
                }
                else {
                    category.Categories = category.Categories.filter(function (subcategory) { return recursiveFilter(subcategory); });
                    return subcategory.Categories.length > 0;
                }
            }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top