Javascript Objects: Dynamically create properties and property names

4.5k views Asked by At

I'm trying to dynamically create objects within objects that all have dynamic property names and properties. Here is a sample:

jobs:

|  name  |  job  |  building  |
 -----------------------------
|  adam  |  mop  |  school    |
|  adam  | teach |  school    |
|  eve   | cook  |  kitchen   |
|  eve   |  mop  |  house     |

dataNames:

| name  |
---------
| adam  |
| eve   |

Desired result:

names = {
    adam: {
        school: ["mop", "teach"] 
    },
    eve: {
        kitchen: ["cook"],
        house: ["mop"]
    }
}

The script is used in google's app script, so functions like eval() can not be used. Here is my current code:

  namesSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('dataNames');
  namesValues = namesSheet.getDataRange().getValues();
  jobsSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('jobs');
  jobsValues = rolesSheet.getDataRange().getValues();
  jobsNumRows = rolesRange.getNumRows();

var names = {};
  for (item in namesValues) {
    //create new entry in names array with key name
    var name = namesValues[item];
    names[name] = {};                                  //<-- so far, so good

    //cycle through jobs sheet and find all buildings and jobs tied to name
    for(i=1;i<jobsNumRows;i++){

      var jobsName = jobsValues[i][0];
      if(jobsName == name){
        var buildingName = jobsValues[i][2];
        var jobDesc = jobsValues[i][1];           

        //Check if names.name already has a building property
        if(!names.name[buildingName]){
            names.name[buildingName] = []; //<-- doesn't work
        }

         names.name[buildingName].push(jobDesc);  <--probably wouldn't work...?

      }
    }
  } 
}

How can I set this up? I've looked at all the SO questions about creating dynamically named properties in objects. This is slightly different in that were doing this twice: a dynamically named property of a dynamically named property. Unless of course, that isn't different?

Thanks SO!

2

There are 2 answers

1
Tony Nardi On BEST ANSWER

I believe these lines should read:

    //Check if names.name already has a building property
    if(!names[name][buildingName]){
        names[name][buildingName] = []; //<-- doesn't work
    }

     names[name][buildingName].push(jobDesc);  <--probably wouldn't work...?
1
nomier On

Try:

if(!names[name][buildingName]){
    names[name][buildingName] = [];
}

names[name][buildingName].push(jobDesc);

Using the '.' operator needs the actual value of the variable, so you can't do something like:

var x = "variable";

var obj = {};
obj.x = {};

This means that obj will have a property named x and it has nothing to do the previously defined variable.

Instead you should use this:

var obj = {}
obj[x] = {}