Object attributes can be assigned by dot notation, or the bracket notation (like Arrays)
So try this instead:
this[name] = name;
You wont then get a 'variable' of the department name, but the array of departments will be of objects with departments as attribute. Is this what you want, you potentially don't need the Array for loo if department names will always be unique.
Update
To expand on my point above about the non-need of the extra array. Because you are dealing with (I assume) unique department names, these can be the keys.
So:
function buildDepartments() {
var departmentList = SpreadsheetApp.getActiveSpreadsheet().getRange('a1:c2').getValues(), // Assuming a 3 column list with, say: department name, department head, department deputy.
departments = {},
d;
for (d = 0; d < departmentList.length; d += 1) {
departments[departmentList[d][0]] = {head: departmentList[d][1], deputy: departmentList[d][2]};
}
// To recall the department details ... however many there are
Logger.log(departments['Math'].head); // Logs the head of the Math department
}
but, it may make more sense to not tie yourself into keys that may change over time as if the department name changes it will be a hassle to make changes. It will also be easier to utilise ScriptDB if you 'despecify' the object.
Thus:
function buildDepartments() {
// Assuming a 3 column list with, say: name, head, deputy.
var departmentList = SpreadsheetApp.getActiveSpreadsheet().getRange('a1:c2').getValues(),
departments = [], // Now this an array
d;
for (d = 0; d < departmentList.length; d += 1) {
departments.push({
title: departmentList[d][0],
head: departmentList[d][1],
deputy: departmentList[d][2]
});
}
// To recall the department details ... however many there are
// Logs the head of the Math department by filtering the array to a single entry
Logger.log(departments.filter(function (d) { return d.title = 'Math'; })[0]);
}
At this point, and to make things a little more intuitive on recall the departments array could be a constructor object once again. Thus:
function DepartmentList () {
var list = [];
this.addDepartment = function (dept) {
list.push({ title: dept[0], head: dept[1], deputy: dept[2] });
return this; // for chaining reasons, if you like.
}
this.getDepartment() = function(dept) {
var foundDepartment = list.filter(function (d) { return d.title = dept; });
return foundDepartment[0] || {}; // empty object if no department match
}
return list; // bare calls to the object returns the whole list
}
function buildDepartments () {
var departments = SpreadsheetApp.getActiveSpreadsheet().getRange('a1:c2').getValues(),
departmentList = new DepartmentList(), // Now this an object again
d;
departments.forEach( departmentList.addDepartment(entry) ); // Possible because GAS supports ECMAScript5
// To recall the department details ... however many there are
// Logs the head of the Math department
Logger.log(departmentList.getDepartment('Math').head);
}
The latter metod will only start to make sense if the input data is more extensive or needs any processing by object functions. This is probably overkill for my stated example.