0

this is my code, data grabs a json response from ajax. when I do console.debug ...if you click on it to expand this is what it shows:

181818
0.10926253687316
303030
0.054454277286136
d8a890
0.091268436578171
d8d8d8
0.22377581120944
f0d8c0
0.3269616519174 

and this my code:

data = $.parseJSON(JSON.stringify(a));

            console.debug("Here is blah: %o", data);

            var myArray = data;
            alert(myArray);
    for (var i=0,  tot=myArray.length; i < tot; i++) {
      console.log(myArray[i]); //"aa", "bb"
    }

I am trying to loop through the array and log the pair to the console something like : 181818 = 0.1092 etc..any ideas/suggestions please?

4
  • What is the point of this line? data = $.parseJSON(JSON.stringify(a)); You might as well write data = a, unless you are actually trying to make a deep copy of a. Commented Jan 29, 2014 at 7:04
  • a is a json string received by an ajax query Commented Jan 29, 2014 at 7:06
  • Then why are you JSON.stringify()ing it? At any rate, jQuery will parse the JSON for you in most cases. Commented Jan 29, 2014 at 7:07
  • sorry my bad. what do you suggest Commented Jan 29, 2014 at 7:08

1 Answer 1

3

Something like

for (var key in data) {
       console.log(key+" = "+data[key]);
}

Remember that data is an object not an array. So in your php code you can just output an array like this

echo json_encode($someArray);

Where

$someArray[181818] = 0.10926253687316;

and so on. Next you can modify as it according to your needs.

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

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.