简体   繁体   中英

Javascript Objects: Dynamically create properties and property names

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!

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] = {}

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...?

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