Context
I'm building a 3 tier nested hierarchy dropdown to display the location of certain IoT devices within a building. The Floors are at the top of the hierarchy and grandparents of the Level object, followed by Units as parents, and then Room and Point Of Use as children See image as reference below:
3 Tier Nested Hierarchy Dropdown Tree
I first make a request to retrieve all of the devices for a building, which comes back like this: An array of objects, all containing properties with keys relating to their placement within the building.
(3) [{…}, {…}, {…}]
0:
label: "floor_1_unit_2_kitchen_sink"
properties:
level: "Floor"
level_no: "1"
pou: "_Sink"
room: "_Kitchen"
unit: "_Unit"
unit_no: "_2"
_config: {pou: {…}, room: {…}, unit: {…}, level: {…}, unit_no: {…}, …}
1: {label: 'floor_2_unit_1_kitchen_sink'}
2: {label: 'floor_1_unit_1_kitchen_sink'}
Using the above data as reference, you can see that there can be multiple devices on a single floor.
What I need to do
I need to create groups for the Level objects according the Level objects grandparent property values that are equal to one another ("Floor 1","Floor 2"). After that, I would like to then assign each objects parents and children to a group according the that child's grandparent property value (all parents & children with a grandparent value of 1, go to the "Floor 1" group).
What I've tried
I have created a tree like data structure that houses the same data above.
(3) [Level, Level, Level]
0: Level
children: Units
children: Array(3)
0: {name: '_Unit_2_Kitchen_Sink', device_label: 'floor_1_unit_2_kitchen_sink'}
1: {name: '_Unit_3_Kitchen_Faucet', device_label: 'floor_2_unit_1_kitchen_sink'}
2: {name: NaN, device_label: 'odeus-dev-board-1'}
3: {name: '_Unit_1_Kitchen_Sink', device_label: 'floor_1_unit_1_kitchen_sink'}
key: "_Unit"
parent: "Floor"
value: "_2"
key: "Floor"
parent: null
value: "1"
1: Level {key: 'Floor', value: '2', parent: null, children: Units}
2: Level {key: 'Floor', value: '1', parent: null, children: Units}
I am still a beginner and have never worked with object traversal and am finding it difficult to come across more complicated object methods to work with. Am I going about this the wrong way? Any help is greatly appreciated!