1

I am trying to refer to a variable value from both a generic-variable Name (like "Mike") and an unambiguous-variable-name like "myNeighbor_Mike_age".

The Database entry comes with the ID as name like:

dbObject =
   {
   MyNeighbor_Mike_age:21,
   MyFriend_Thomas_age:404,
   MyNeighbor_Nik_age:30
   }
]

I want a generic structure so that i can write functions for e.g. the myNeighbor-objectType of which I may have multible ( like: "closeNeighbor"; "neighborNextStreat"):

newPersonObject =
{
   "myNeighbor":{
      "Mike":
         {"age":null},
         {"ID":MyNeighbor_Mike_age},
      "Nik":
         {"age":null}
         {"ID":MyNeighbor_Nik_age},
   }
   "myFriend":{
      "Thomas":
         {"age":null}
         {"ID":MyFriend_Thomas_age},
   }
}

I am looking for a way to link the dbObject name to the coresponding object of newPersonObject. So I have do o the "linking work" only once and can then just use the full name (i.e. as part of a linking-function name) to set the values.

I thought I coud reference it within the main-object like:

   newPersonObject.myNeighbor.Mike.age = dbObject.MyNeighbor_Mike_age

In the programm I used computed properties to dynamically set the value so it looked more like this:


peopleType = Object.keys(newPersonObject)[0]
name = Object.keys(newPersonObject[peopleType][0])
// used a for(-of)-loop but for simplicity 
newPersonObject[peopleType][name]["age"] = dbObject[newPersonObject[peopleType][name].ID]

so that I can access the variable without needing to parse the DB-variable-name (runtime and hardware resources). I.e.

newPersonObject[MyNeighbor_Mike_age] = dbObject[MyNeighbor_Mike_age] // =21
// and have also set newPersonObject.myNeighbor.Mike.age to 21

But it didn´t work.

I came up with the following "solution" which i think is not optimal, bc. i have to call a function and it seems counterintuitive to call

newPersonObject.<variableName>(xxx) to set a value:
newPersonObject[personObject.ID] = function(ageValue){
   this[typeOfPerson][name].value = ageValue; 
};

I currently set the values via a loop like this:


for(let objectKey of Object.keys(dbObject)){   
    newPersonObject[objectKey ](dbArray[objectKey])
    //objectKey should return 
    //MyNeighbor_Mike_age
    //myFriend_Thomas_age
    //MyNeighbor_Nik_age
}

I am using Node-Red so i would preferably not add extra packages or use global-variables.

Any idea, how to tidy things up?

Thanks in advance

hardillb
  • 54,545
  • 11
  • 67
  • 105
Ari
  • 11
  • 3
  • Why do you need to put `MyNeighbor_Mike_age` in the `newPersonObject` as well? I'd keep it inside the `dbObject` only, for simplicity in distinguishing them. – Bergi May 11 '21 at 14:56
  • @Bergi The `dbObject` is a response from a S7 controller via PUT/GET or Modbus so basically a database response. I can´t alter the information-structure. The reason i put `MyNeighbor_Mike_age` into the `newPersonObject` was to have the link between the generic variable name (e.g. `MyNeighbor.Mike.age`) and the unambiguous name (`MyNeighbor_Mike_age`) in one place, to ease the data parsing. – Ari May 12 '21 at 05:45
  • Do you want to link the *name* or the *value*? And what do you mean by "*ease the data parsing*" - isn't the conversion of `dbObject` to `newPersonObject` the parsing? – Bergi May 12 '21 at 11:23
  • When i get the data i only have their names to work with. I probably could use something like split() to get the value properties out of the name and the select the corresponding object. But that would be a lot of workload every time. My plan was to use the name of the data i get (as string) to directly call a function or similar. So while im not 100% sure what you mean i would state i want to link the *value* The construction of `newPersonObject` would happen in a initial step on startup – Ari May 12 '21 at 12:09
  • Sounds like you simply want `newPersonObject[peopleType][name].age = dbObject[newPersonObject[peopleType][name].ID]` (instead of `newPersonObject[newPersonObject[peopleType][name].ID]`)? – Bergi May 12 '21 at 12:10
  • yes. I will correct it. – Ari May 12 '21 at 12:18

1 Answers1

0

Ok looks like I overcomplicated things. As @Bergi pointed out the following was more usefull: instead of refering to the values i now just do a loop over the relevant properties

// for each group like "neighbor";"friend"
for(let peopleType in newPersonObject){
   //for each name like "Mike" "Nik" "Thomas"
   for(let name in newPersonObject[peopleType]){
           let unambiguousID = newPersonObject[peopleType][name].ID
           newPersonObject[peopleType][name].age= dbObject[unambiguousID];
       }
}

It is easier and cleaner than throwing in a custom funktion, like my first idea. So far it does not look like the performance hit is extreme nor significant from 1-3 ms to 1-4 ms so far for 1000 people of 5 Types.

Ari
  • 11
  • 3