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