我在将新对象文字添加到数组中时遇到问题。当我使用数组名称时,一切正常。但是,当我切换代码以使用包含数组名称的变量时,它不起作用。
我有6个阵列,如下所示,它们列在我的页面上。单击将保存单击的数组名称保存到名为whichList的变量中。
以下是三个阵列:
student0 = [
{
status: "completed",
goal: "go to store",
duedate: "November 1",
datecreated: ""
}, {
status: "completed",
goal: "buy beer",
duedate: "November 2",
datecreated: ""
}
];
student1 = [
{
status: "completed",
goal: "go to the beach",
duedate: "November 7"
}, {
status: "completed",
goal: "swim without drowning",
duedate: "November 8",
datecreated: ""
}
];
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();
student0[student0.length] = {
status: "pending",
goal: "\"" + $('#thegoal').val() + "\"",
duedate: "\"" + $('#thedeadline').val() + "\"",
datecreated: "\"" + datecreated + "\""
};
return console.log(student0);
});
这是非工作代码。我想使用whichList变量。
我已经使用console.log来检查变量是否在函数开头显示正确的数组名称。一切都很好。但是我在控制台中得到的只是数组变量,而不是我在工作版本中所做的数组内容。
$('#savegoal').click(function() {
datecreated = new Date();
whichList[whichList.length] = {
status: "pending",
goal: "\"" + $('#thegoal').val() + "\"",
duedate: "\"" + $('#thedeadline').val() + "\"",
datecreated: "\"" + datecreated + "\""
};
return console.log(whichList);
});
答案 0 :(得分:3)
您可以使用window[whichList]
,因为数组变量可能位于全局/窗口范围内:
var whichList = "student0";
var theList = window[whichList];
theList[theList.length] = { ... }; // consider using instead: theList.push( { ... })
答案 1 :(得分:0)
直接回答您的问题:
var wichList = null;
$('#savegoal').click(function() {
if (wichList == null){
alert('No list selected');
}
// don't forget the "var", otherwise "datecreated"
// will actually be a global variable
var datecreated = new Date();
// ".push()" adds the given value at the end of the array
wichList.push({
status: "pending",
goal: "\"" + $('#thegoal').val() + "\"",
duedate: "\"" + $('#thedeadline').val() + "\"",
datecreated: "\"" + datecreated + "\""
});
return console.log(wichList);
});
//somewhere else in your code :
if (somecond) {
wichList = student0;
} elseif (somecond) {
wichList = student1;
} else {
wichList = null;
}
以上代码有效:在javascript中,array
变量实际上是数组内容的引用
var a = [];
var b = a;
// b an a are now references to the same array,
// any modification to a will be "seen" by b, and vice versa :
a.push(1);
b.push(2);
// a == b == [1,2]
但是,考虑到“学生”变量的名称,这些列表可能应存储在数组中:
var students = [];
students[0] = [{
status: "completed",
goal: "go to store",
duedate: "November 1",
datecreated: ""
}, {
status: "completed",
goal: "buy beer",
duedate: "November 2",
datecreated: ""
}];
students[1] = [ ...
现在,您可以使用此数组中的索引作为识别学生的方法,可能需要额外选择:
<select id="student">
<option value="0">student 0</option>
<option value="1">student 1</option>
...
</select>
并在click
回调中使用它:
$('#savegoal').click(function() {
var datecreated = new Date();
var i = $('#student').val();
students[i].push({
status: "pending",
goal: "\"" + $('#thegoal').val() + "\"",
duedate: "\"" + $('#thedeadline').val() + "\"",
datecreated: "\"" + datecreated + "\""
});
return console.log(students[i]);
});
答案 2 :(得分:-1)
首先,使用数组而不是单独的变量:
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: ""
}],
...
];
然后,whichList
应该是数组的索引(即0到5之间的数字)而不是变量的名称,你可以这样做:
$('#savegoal').click(function() {
datecreated = new Date().toString();
student[whichList].push({
status: "pending",
goal: $('#thegoal').val(),
duedate: $('#thedeadline').val(),
datecreated: datecreated
});
});
您不需要连接所有这些"\""
- 在键入字符串文字时,引号是符号的一部分,您不需要它们从变量或表达式获取字符串。
如果您想使用学生ID作为寻找每个学生的关键,您可以采取以下措施:
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);
});