0

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 Answers 2

2

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] = {}
Sign up to request clarification or add additional context in comments.

1 Comment

You are absolutely correct. @TonyNardi just answered similarly. Unfortunately I can only mark one answer as correct, but here's an upvote!
1

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 Comment

Genius! Thanks man. Will mark correct in 3 minutes (S.O. rules)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.