1

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.

5
  • u will have to show the returnedData here.! Commented May 8, 2013 at 14:40
  • 3
    You need to put the call to "drawGraph()" inside the $.get() callback function. Commented May 8, 2013 at 14:40
  • 5
    u did not initialize variable dataa Commented May 8, 2013 at 14:41
  • Initialise dataa to an empty string before using it in the loop. Why does the second loop assume 6 items? Also, though it isn't causing the problem you mentioned, you should declare all of your variables with var or they'll be globals - which could lead to other problems later. Commented May 8, 2013 at 14:42
  • 2
    What are you building the string for? Looks like using JSON.stringify would be simpler. Commented May 8, 2013 at 14:43

3 Answers 3

5

When you do this:

for  (var a=0;a<6;a++){
    mytext = "x:" +x[a] + "," + "y:" +y[a] + "}" + "," + "{" ;
    dataa=dataa+mytext; 
}   

you're performing string concatenation with dataa and mytext. However, in the first iteration of that for loop dataa has not yet been initialised, and the string representation of that is undefined.

When you declare the dataa variable initialise it as an empty string instead:

var mytext,dataa = '',f;

As an aside, it's important to remember that $.getJSON() makes an asynchronous request for data. If the result of that call is necessary for the drawGraph function to perform correctly then your code isn't going to function as you might expect (though with what you've posted in the question there's no definitive indication this is the case).

The $.getJSON() function will finish executing immediately (after initiating the request), allowing drawGraph to execute, but there's no guarantee that the request will have received a response and the processing of returnedData will have occurred; in fact there's a very strong possibility that won't have happened.

Sign up to request clarification or add additional context in comments.

1 Comment

It's also going to be important to deal with the asynchronous nature of the $.get() call.
1

You shouldn't be building an object through string concatenation like that. I'd recommend actually building up an array of objects like so:

var dataa = [];
for (var a = 0; a < 6; a++) {
    var newObj = {x: + x[a], y: + y[a]};
    dataa.push(newObj);
}

And then to generate the f you currently have:

var f = JSON.stringify([{data: dataa}]);

3 Comments

This is definitely more than What I have expected.. Amazing! thanks a lot colin !!!!
Hi. I have some problem with the result. the output is [{"data":[{"x":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}]}] ... I want it like the same without quotes.. can u help me pls?
What are you trying to use the string for?
0
    dataa = dataa + mytext;
--------------^

this is undefined initially

You need to do this -

 var mytext, dataa = "", f;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.