4

Edit This is the function where I get the response from

$(document).ready(function()
{

  $.ajax({
    method: "get",
    url: 'ctr_seearmylist.php',
    dataType: 'jsonp',
    data: 'get=squad',
    success: processSquads
  });

});

and this is the php snippet that creates the response:

{..... //iterates throuh a result taken from the database
  $temp[0]=$id;
   $temp[1]=$squad_id;
   $result[]=$temp;
  }
  $result=json_encode($result);
  }
return $result;
}

if i call alert(response.constructor); I get

function Array() {
    [native code]
}

End Edit

How do I iterate through a json array using jquery or javascript, or whatever works?

the json response i get has this form: [["1","12"],["2","3"],["3","7"]]

I should mention that using response.length; has no effect

function processSquads(response)
{
  alert (response[0][0]); // works and returns 1 
  alert (response[0]); // works and returns 1,12
  alert (response.length); //doesn't work so I can't iterate 
}

Sorry for the large number of questions today, but I'm just getting started with Ajax and I get stuck.

5
  • There's no JSON anywhere in your question. It's a Javscript question, not JSON Commented Jan 16, 2011 at 20:54
  • 1
    Are you sure the response isn't '{"0":["1","12"],"1":["2","3"],"2":["3","7"]}'? Commented Jan 16, 2011 at 20:58
  • What do you get if you do alert(response.constructor);? Commented Jan 16, 2011 at 21:09
  • It's surely as I write it. And when I write alert(response.constructor) I got function Array() { [native code] } Commented Jan 16, 2011 at 21:17
  • I managed to make it work although I have no idea what of the changes I made made it work. I am sure it was a ', ) ; or something of the sort :( Commented Jan 16, 2011 at 21:25

4 Answers 4

5

With Jquery:

var arr = [["1","12"],["2","3"],["3","7"]];
jQuery.each(arr, function() {
  alert(this[0] + " : " + this[1]);
});
//alerts: 1 : 12, etc.

This iterates the array and then shows what is in index 0 and 1.

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

Comments

1

that's not a json array, it's an array of arrays

this should work fine: http://jsfiddle.net/w6HUV/2/

var array = [["1", "12"], ["2", "3"], ["3", "7"]];

processSquads(array);

function processSquads(response) {
    alert(response[0][0]); // 1
    alert(response[0]); // 1, 12
    alert(response.length); // 3

    $(array).each(function(i){
        alert(response[i]); // 1,12 - 2,3 - 3,7
    });
}

1 Comment

I edited the post with some more details. I now it looks like an array of arrays but surely I can't find the length of it. the exact code you wrote -and I know is correct, just doesn't work
0

Untested but this should work:

function processSquads(response)
{
  for(var list in response)
  {
    for(var item in response)
    {
      alert(item);
    }
  }
}

Comments

0

Not sure why jQuery answers are posted here, but you should find out why the length property is not working when it should. Posting the jQuery code from one of the answers with hazelnut JavaScript.

var arr = [["1","12"],["2","3"],["3","7"]];
for(var i = 0; i < arr.length; i++) {
    var item = arr[i];
    console.log(item[0] + " : " + item[1]);
}

Can you post an reproducible example of what you're doing on jsfiddle or some other site?

1 Comment

Its tagged jquery and he also asked for either jquery or javascript.

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.