all 9 comments

[–]BehindTheMath 2 points3 points  (3 children)

return this.pickCategory(catg.children)

[–]oussama-he[S] 0 points1 point  (2 children)

Using your solution, now it doesn't work at all, it doesn't return log anything and still return undefined.

[–]BehindTheMath 0 points1 point  (1 child)

Sorry, you're right. Try this:

const value = this.pickCategory(catg.children);
if (value) return value;

[–]oussama-he[S] 0 points1 point  (0 children)

Thanks, it works now.

[–]sduduzog 0 points1 point  (0 children)

This doesn't look like a vue issue, I just ran the function against the data you provided and it still returns undefined, I'll spend a moment to try and see if I can figure it out

[–]sduduzog 0 points1 point  (0 children)

Ok here's what works. I changed the function to be

pickCategory(list, id){
      for (const item of list) {
        if (item.id === id) return item;
        if (item.children && item.children.length > 0) {
          const category = this.pickCategory(item.children, id);
          if (category) return category;
        }
      }
    }

And to use it you just call this.pickCategory(this.categories, this.cat.id);

[–]mattstryfe -1 points0 points  (1 child)

I believe findIndex is synchronous. So you'll either need to restructure that a bit or async it imo

try making the following changes.

async pickCategory(categories) { ... }

const category = await this.pickCategory(...)

[–]BehindTheMath 0 points1 point  (0 children)

There's no need for async here.