Daniel
Daniel

Reputation: 4342

javascript push to 1 array

I am trying to push new values to an array, but when the values are pushed 2 new arrays are created. I am trying to push the values into one array not in separate ones. How do I do that? Here is the code I am using.

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) });
  }
}

Upvotes: 0

Views: 81

Answers (2)

SomeKittens
SomeKittens

Reputation: 39522

Every time you execute the loop, the line totalCodes = new Array() is called, resetting totalCodes to an empty array. Declare totalCodes outside of your for loop.

Upvotes: 0

Kalpesh
Kalpesh

Reputation: 5685

Move this totalCodes = new Array(); before your for loop.

Each time your for loop is iterating, it's creating new array with totalCodes = new Array();.

Upvotes: 2

Related Questions