So I am grabbing this XML data, and parsing it within the success function of this jQuery.ajax call.
var d1 = [];
jQuery.ajax(
{ url: ("/charts/quotes/" + name + ".xml"),
success: function( data )
{
var dString; var qString; var d; var q;
jQuery(data).find("HistoricalQuote").each(
function () {
dString = $(this).children("Date").text();
qString = $(this).children("LastClose").text();
d = Date.parse(dString);
q = parseFloat(qString);
d1.push( [ d, q ] );
console.log( d1[d1.length-1][0] + ": " + d1[d1.length-1][1] );
/* ^ First Log ^ */
} );
console.log( d1.length );
for ( var q in d1 )
{
console.log(q[0] + ": " + q[1]);
/* ^ Second Log ^ */
}
},
async: false /*The success function must complete before we continue.*/
});
Now, that first log is giving me back the data that I expect it to — “d” is a really long integer, that looks like it might be a timestamp, and “q” is a stock quote — a float somewhere around 26.
Here’s the weird thing. Outside of that each loop (“Second Log”) — I get a vastly different set of data. It starts off with:
(Sorry for the line returns… Stack Overflow was doing something odd to it)
0: undefined
1: undefined
2: undefined
3: undefined
4: undefined
5: undefined
6: undefined
7: undefined
8: undefined
9: undefined
…and then goes on to:
1: 0
1: 1
1: 2
…
1: 9
2: 0
…
3: 9
4: 0
Does this have something to do with the way I looped through it the second time? Or the way “push” works? What in the world is going on?
qis actually going to be the index of item in d1, not the value. You would want to do something like:However using a for/in is a bad idea on a numerically indexed array… see the link in heikki’s comment.