2012-08-06 105 views
0

我正在嘗試將新值推送到數組,但是當值被推送時會創建2個新數組。我試圖將值推入一個不在單獨的數組中。我怎麼做?這是我正在使用的代碼。javascript push to 1 array

for (count = 0; count < xmlItem.length; count++) { 
    // dates 
    xmlDate = new Date(xmlItem[count].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue); 
    // titles 
    xmlTitle = xmlItem[count].getElementsByTagName("title")[0].childNodes[0].nodeValue; 
    // descriptions 
    xmlDescription = xmlItem[count].getElementsByTagName("description")[0].childNodes[0].nodeValue; 
    // date reformated 
    pubDate = (xmlDate.getMonth() + 1) + "/" + xmlDate.getDate() + "/" + xmlDate.getFullYear(); 
    // if there is a new code 
    if (pubDate == today) { 
     // array with titles for new values 
     totalValues = new Array(); 
     // add the title 
     totalValues.push(xmlTitle); 
     // add badge for number of values 
     chrome.browserAction.setBadgeText({ text: JSON.stringify(totalValues.length) }); 
    } 
} 

回答

2

在您的for循環之前移動此totalCodes = new Array();

每次您的for循環迭代時,它都會創建一個新的數組,其中totalCodes = new Array();

+1

或'totalCodes = [];' – Don 2012-08-06 16:23:13

+0

謝謝!工作! – 2012-08-06 16:23:45

+0

你能接受這個答案嗎? – Kalpesh 2012-08-06 16:34:49

0

每次執行循環時,調用行totalCodes = new Array(),將totalCodes重置爲空數組。在for循環外聲明totalCodes