I am trying to create a format for a graph. When I try to do so I am getting an undefined with console.log before the data. Here's my code
$(document).ready(function () {
var graphData = new Array();
$.getJSON("ds/js.json", function (Data) {
dataLength = returnedData.data.length;
var x = new Array();
var y = new Array();
var mytext, dataa, f;
for (i = 0; i < dataLength; i++) {
x[i] = Data.data[i].avgPrice;
y[i] = Data.data[i].numProducts;
}
for (var a = 0; a < 6; a++) {
mytext = "x:" + x[a] + "," + "y:" + y[a] + "}" + "," + "{";
dataa = dataa + mytext;
}
console.log(dataa);
var f =
"[{data : [{" + dataa;
console.log(f);
});
drawGraph(graphData);
});
Console output :
undefinedx:87.6,y:85},{x:116.08,y:61},{x:113.11,y:49},{x:181.37,y:65},{x:138.14,y:74},{x:66.03,y:89},x:66.03,y:89},{
What am I doing wrong here? And also I want a separate format for a=5, that stops the ", {" coming at the end.
returnedDatahere.!$.get()callback function.dataato an empty string before using it in the loop. Why does the second loop assume6items? Also, though it isn't causing the problem you mentioned, you should declare all of your variables withvaror they'll be globals - which could lead to other problems later.JSON.stringifywould be simpler.