4

新しいオブジェクト リテラルを配列に入れるのに問題があります。配列名を使用すると、すべてが機能します。しかし、配列名を保持している変数を使用するようにコードを切り替えると、機能しません。

次のような6つのアレイがあり、それらは私のページにリストされています。クリックすると、クリックされた配列の名前が whichList という変数に保存されます。

以下に 3 つの配列を示します。

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

You can use window[whichList], since the array variable is presumably in the global/window scope:

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 に答える