Nisha Patel
Problem
So I have a javascript array that looks like this (simplified). Here beauty (id:1) and health (id:2) is root categories as they have a null value of parentCategoryId. hair care (id:3), hair oil (id:4) and kumarika hair oil (id:5) falls into the beauty category(id:1) as they have parentCategoryId value which directly or indirectly falls into beauty category(see below for explaination).But supplements (id:6) falls into the health category (id:2). How do I get all the categories that (directly or nested) falls into the beauty category (id:1). key parentCategoryId is the identifier of which category directly falls into which category. so here
id:3 has parentCategoryId:1 that means id:3 is direct children of id:1.So falls into beauty category.
id:4 has parentCategoryId:3 that means id:4 is direct children of id:3 which is direct children of id:1.so falls into beauty category.
id:5 has parentCategoryId:4 which means id:5 is direct children of id:4 which is direct children of id:3 which is direct children of id:1 and falls into beauty category.
I have tried recursive function but couldn't find the appropriate output.Say for example I am calling this recursive function with the object that has id:1.
My expected output should be an array of categories that are direct or nested children of beauty (id:1 ). So the new array should look like this. Please take into consideration that there may exist more nested categories.
Solution
You have some interesting options already, provided categories aren't a very long list (like thousands of entries). (They do lots of looping through the list.)
If there are a lot of categories in the list (at least thousands), but the hierarchies aren't hundreds deep, I'd probably just loop through categories once, checking the ancestry of each target category as I went.
I'd start by keeping a map of categories by their ID (just after the categories array unless that's dynamic; if it's dynamic, create the map on the fly):
Then get the target category ID (since you seem to be starting with a category object) and an array for the results:
Then loop through the categories:
In the loop, we'll go through the cat's ancestors (if any) to see if any of them match the target:
and that's it!
Working example (rather shorter without the explanatory comments):
Suggested blogs:
>How to use querySelectorAll()" with multiple conditions in JavaScript?
>How to fix mouseover event glitch in JavaScript?
>How to do light and dark mode in a website using HTML and JavaScript?
>How to manipulate manipulating Array object in JavaScript?
>How to merge an object into Array with the same key in JavaScript?
>Javascript Error Solved: Property 'id' does not exist on type 'T'
>Why highlighted table row using class not working in JavaScript?
>How to rename an object key based on the condition in JavaScript?
>How to sort an array based on another array in Javascript?
>Javascript: Modal not closing with a button