简体   繁体   中英

JS: referencing to Child of "Sibling"-object value in multi layer object

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 eg 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 (ie 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). Ie

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM