4

我无法将新的对象文字放入数组中。当我使用数组名称时,一切正常。但是,当我切换代码以使用包含数组名称的变量时,它不起作用。

我有 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);
});
4

3 回答 3

3

您可以使用window[whichList],因为数组变量可能在全局/窗口范围内:

var whichList = "student0";
var theList = window[whichList];
theList[theList.length] = { ... };   // consider using instead: theList.push( { ... }) 
于 2013-10-16T22:11:11.197 回答
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 :

<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]);
});
于 2013-10-17T08:15:03.120 回答
-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);
});
于 2013-10-16T23:52:57.403 回答