0

I want to know how can I retrieve the values from the json if the json returns an array of result?

Note: the first array is not the same length of the second array but the first array is will be always be less than the second array

Here's what I do as of now and get this error:

enter image description here

Here's my jquery code:

success: function(data){
            var toAppend = '';
            if(typeof data === "object"){
                for(var i=0;i<data.length;i++){
                    toAppend += '<tr><td colspan="2">'+data[0][i]['m-asin'][0]+'</td></tr>';
                    toAppend += '<tr><td>'+data[1][i]['asin'][0]+'</td><td>'+data[1][i]['size'][0]+'</td></tr>';
                }
                $('.data-results').append(toAppend);
            }
        }

ANd I think no need for php code since it is working because it returns the expected results. Any help will be a big thing thanks!

1 Answer 1

2

data[0][i] gives you the i-th property of the first data element. You'll need to revert it: data[i][0].

Or otherwise, you'll need to alter the loop, to get the number of elements in data[0]:

for(var i=0; i<data[0].length; i++){
Sign up to request clarification or add additional context in comments.

3 Comments

data[i][1] is undefined I get this result :#
Yeah, the relationship between your code and and the JSON data wasn't exactly clear to me, but I think you only need to alter the for loop. You will then get the length of data[0], so you can then ask for data[0][i] and data[1][i]. In your posted code you get the length of data (top level), which is 2.
if remove the data[0][i]['m-asin'][0] the data[1][0]['asin'][0] will display but I really need to display it both how should I do this

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.