0

I'm trying to access data in a multi-level AJAX response sent by PHP (WP). I've read other q's on the topic that say I need to iterate through the result so I am trying this:

$j.each(result[0], function(key , value){ 
   // key, value
     $j.each(value, function(k , v){ 
        k,v
    })
})

...which gives me the below in console:

array example

I need to access just the values of each array so that they end up like this:

["2017-12-16", 128],
["2017-12-17", 105],
["2017-12-18", 76],
["2017-12-19", 107],
["2017-12-20", 93],
["2017-12-21", 46]

What I don't understand is after going down to a second .each why the result is the same as the first .each (both are same as image above). Could do with pointing in the right direction please. Many thanks.

Update I made the result array I wanted into an object and accessed it as follows:

obj = result[0];
$j.each( obj, function( key, value ) {
   '["'+value[0]+'",', value[1]+'],'
})

...but this all became a moot point anyway as I was trying to add rows to a Google Chart and didn't need to iterate I just added the obj array and all was good.

1 Answer 1

1

You can convert this JSON result set into an array like this:

// Define results array
output=[];

// Iterate through objects in set, convert, and add to array
for (i in result[0]) output[i]=$.map(result[0][i], function(v, k){ return v; });

I do not have access to your original data set, of course, but I read what you posted to mean that the JSON lives in result[0]

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

1 Comment

Thanks sorak, I ended up using a different method but will try yours as well thanks.

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.