我在這裏考慮了很多主題,考慮到主題,但解決方案似乎不適用於我。我想我有一些邏輯問題。我的代碼非常簡單。我有兩個數組(第一個包含三個字符串和第二個包含三個日期)循環遍歷每次迭代並將其保存到數組中
所有我想要做的就是第一個字符串存儲在一個對象第二陣列中的第一次約會的第一陣列英寸
與所述陣列I中的函數theMainFunc創建(代碼中的問題)的保存3個對象與每個陣列的最後一個索引,而不是匹配他們的像我需要。
下面是代碼:
var dlcNameList = ['they shall not pass', 'in the name of the tsar', 'apocalypse'];
var dlcReleaseDate = [new Date(2017,2,28), new Date(2017,6,15), new Date(2017,11,25)];
function objectCreator (dlcObject,dlcName,dlcDate) {
\t \t dlcObject.name = dlcName;
\t \t dlcObject.date = dlcDate;
\t \t return dlcObject;
}
function theMainFunc() {
\t var dlcDetails = {};
\t var i = 0;
\t var storage = [];
\t var x;
\t for (i; i <= 2; i++){
\t x = objectCreator(dlcDetails,dlcNameList[i],dlcReleaseDate[i]);
\t storage[i] = x;
\t }
\t return storage; //This returns an array with three "Same" objects. Not what I want to really acheive
}
console.log(theMainFunc())
不知道你的意思是「你不必創建每個迭代的新對象」,爲文本創建一個新的對象,它只是它內聯。 –
@JustinBlank他正在作出新的對象'VAR dlcDetails = {}'與每個迭代**,然後**給它分配的屬性。這基本上不一定在這種情況下。 –