0

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!

Liv
  • 15
  • 6
  • How do you want the answer to be in? Do you want to have a tree? Do you want it in hashmap of maybe **level_unit** ? Which do you prefer? – Rajesh Paudel Aug 19 '22 at 10:32
  • @Liv has the answer solve your question? Your question is useful and for that I gave the bounty –  Aug 19 '22 at 13:39

1 Answers1

0

You could create a new object with the floors as main key, then add the units also as objects with the units as keys. You can simply do this by setting the keys to properties.level_no and the same for the units. The below code outputs:

{
  "Floor1": {
    "_Unit_2": [
      {
        "label": "floor_1_unit_2_kitchen_sink",
        "properties": {
          "level": "Floor",
          "level_no": "1",
          "pou": "_Sink",
          "room": "_Kitchen",
          "unit": "_Unit",
          "unit_no": "_2"
        }
      }
    ],
    "_Unit_1": [
      {
        "label": "floor_1_unit_1_kitchen_sink",
        "properties": {
          "level": "Floor",
          "level_no": "1",
          "pou": "_Sink",
          "room": "_Kitchen",
          "unit": "_Unit",
          "unit_no": "_1"
        }
      },
      {
        "label": "floor_1_unit_1_kitchen_fridge",
        "properties": {
          "level": "Floor",
          "level_no": "1",
          "pou": "_Fridge",
          "room": "_Kitchen",
          "unit": "_Unit",
          "unit_no": "_1"
        }
      }
    ]
  },
  "Floor2": {
    "_Unit_1": [
      {
        "label": "floor_2_unit_1_kitchen_sink",
        "properties": {
          "level": "Floor",
          "level_no": "2",
          "pou": "_Sink",
          "room": "_Kitchen",
          "unit": "_Unit",
          "unit_no": "_1"
        }
      }
    ]
  }
}

var inputs = [ {
label : "floor_1_unit_2_kitchen_sink",
properties : {
    level : "Floor",
    level_no : "1",
    pou : "_Sink",
    room : "_Kitchen",
    unit : "_Unit",
    unit_no : "_2"
}
}, {
label : "floor_2_unit_1_kitchen_sink",
properties : {
    level : "Floor",
    level_no : "2",
    pou : "_Sink",
    room : "_Kitchen",
    unit : "_Unit",
    unit_no : "_1"
}
}, {
label : "floor_1_unit_1_kitchen_sink",
properties : {
    level : "Floor",
    level_no : "1",
    pou : "_Sink",
    room : "_Kitchen",
    unit : "_Unit",
    unit_no : "_1"
},

}, {
label : "floor_1_unit_1_kitchen_fridge",
properties : {
    level : "Floor",
    level_no : "1",
    pou : "_Fridge",
    room : "_Kitchen",
    unit : "_Unit",
    unit_no : "_1"
},

} ];
var output = {};
for(var i = 0; i < inputs.length; i++){
    var props = inputs[i].properties;
    if(typeof(output[props.level + props.level_no]) === "undefined"){
        output[props.level + props.level_no] = {};
    }
    if(typeof(output[props.level + props.level_no][props.unit + props.unit_no]) === "undefined"){
        output[props.level + props.level_no][props.unit + props.unit_no] = [];
    }
    output[props.level + props.level_no][props.unit + props.unit_no].push(inputs[i]);
}
console.log(output);

In case you need to sort the floors and units, see this stackoverflow question to find different ways to achieve this.

Klaassiek
  • 2,795
  • 1
  • 23
  • 41