'Nesting arrays into other arrays and then creating a new array
I have a set of components, each with an id that goes from 01 to 50. Each component has a list of components that depend directly on that component. That list is represented as an array. So for, example:
01 = [03, 07, 11, 19, 33, 48]
means that component with id 01 has 6 components that depend directly on it, that is, components 03, 07, 11, 19, 33, 48.
The thing is that a component can also indirectly depend on another component. For instance:
03 = [02, 05, 13, 21]
Here, components 02, 05, 13 and 21 depend directly on 03, but given that component 03 depends on component 01, then 02, 05, 13 and 21 also depend on component 01, just not directly. I need to map both direct and indirect dependency of components, but I am not sure how to go about it. I have 50 components in total, with varying numbers of components depending on them. Ultimately, what I want to achieve is this:
If I have the following arrays,
01 = [03, 07, 11, 19, 33, 48]
03 = [02, 05, 13, 21]
07 = [20, 21, 23, 27, 28, 31, 33, 37, 38]
11 = []
I would like to end up with this:
01 = [02,03,05,07,11,13,19,20,21,23,27,28,31,33,37,38,48]
03 = [02,05,13,20,21,23,27,28,31,33,37,38]
11 = []
Would greatly appreciate input on how to complete this task.
Thanks so much
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
