First use an array instead of individual variables:
var student = [
[{
status: "completed",
goal: "go to store",
duedate: "November 1",
datecreated: ""
}, {
status: "completed",
goal: "buy beer",
duedate: "November 2",
datecreated: ""
}],
[{
status: "completed",
goal: "go to the beach",
duedate: "November 7"
}, {
status: "completed",
goal: "swim without drowning",
duedate: "November 8",
datecreated: ""
}],
[{
status: "completed",
goal: "fly a plane",
duedate: "November 11",
datecreated: ""
}, {
status: "completed",
goal: "don't crash",
duedate: "November 12",
datecreated: ""
}],
...
];
Then it whichListshould be an index in the array (i.e. a number from 0 to 5), not a variable name, and you can do:
$('#savegoal').click(function() {
datecreated = new Date().toString();
student[whichList].push({
status: "pending",
goal: $('#thegoal').val(),
duedate: $('#thedeadline').val(),
datecreated: datecreated
});
});
You do not need to concatenate all of these "\""- quotation marks are part of the notation when typing string literals, you do not need them to get strings from variables or expressions.
Here you can do this if you want to use student IDs as a key to search for each student:
var student = {};
student[student0] = [
{
status: "completed",
goal: "go to store",
duedate: "November 1",
datecreated: ""
}, {
status: "completed",
goal: "buy beer",
duedate: "November 2",
datecreated: ""
}];
student[student1] = [
{
status: "completed",
goal: "go to the beach",
duedate: "November 7"
}, {
status: "completed",
goal: "swim without drowning",
duedate: "November 8",
datecreated: ""
}];
student[student2] = [
{
status: "completed",
goal: "fly a plane",
duedate: "November 11",
datecreated: ""
}, {
status: "completed",
goal: "don't crash",
duedate: "November 12",
datecreated: ""
}];
$('#savegoal').click(function() {
datecreated = new Date();
push(student[whichStudentId],{
status: "pending",
goal: $('#thegoal').val(),
duedate: $('#thedeadline').val(),
datecreated: datecreated
});
console.log(whichList);
});