[英]How to add an object property inside an array using a variable name for javascript?
[英]how to add an object literal to an array using a variable that holds the array name
我在將新的對象文字放入數組時遇到麻煩。 當我使用數組名稱時,一切正常。 但是,當我切換代碼以使用保存數組名稱的變量時,它不起作用。
我有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);
});
您可以使用window[whichList]
,因為數組變量可能位於global / window范圍內:
var whichList = "student0";
var theList = window[whichList];
theList[theList.length] = { ... }; // consider using instead: theList.push( { ... })
您問題的直接答案:
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]);
});
首先,使用數組而不是單獨的變量:
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
});
});
您不需要將所有這些"\\""
連接起來,在鍵入字符串文字時,引號是表示法的一部分,不需要從變量或表達式中獲取字符串的內容。
如果要使用學生證作為查找每個學生的關鍵,請按以下步驟操作:
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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.