JavaScript array length is incorrect -
i have array of data called 'plotdata' consists of number of 'rows' of data, 4-element arrays. array 'plotdata' appended using $.post() script few lines down on in code. '...' of variables shown in post request loaded.
console.log("plotdata (before update): ", plotdata); console.log("plotdata length: ", plotdata.length); ... ... ... $.post( "/csvgen", {node_num: nodes.tostring(), start_time: starttime.tostring(), end_time: endtime.tostring()}, function(data){ var nodedata = $.csv.toarrays(data); nodedata.foreach(function(element){ plotdata.push(element); }); }, "text" ); i logging array , array.length in console, believing these 2 method calls should execute sequentially. when @ console output, console log of array shows there 28 entries, console log of plotdata.length 14. array appended, turns out actual length of plotdata 14 greater plotdata.length console output.
the 'plotdata' array read outside of code, not modified.
what reason this?
edit add log
can't plot empty data set typeerror: cannot read property 'length' of undefined(…) plotdata (before update): [] plotdata length: 0 plotdata (before update): [array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4]] plotdata length: 14 plotdata (before update): [array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4]] plotdata length: 28 plotdata (before update): [array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4], array[4]] plotdata length: 50 the first error b/c dataset empty when instantiate plot.
your code logs before calls post(). try adding logging statements end of callback function -- should show "correct" value (after new rows appended).
Comments
Post a Comment