0

I have a json array with a subarray within it formatted like this:

[{"id":"188","stars":"2","staryes":"2","starers [{"starfirstname":"Name1","starlastname":"Name1","staruser":"1"},
{"starfirstname":"Name2","starlastname":"Name2 ","staruser":"3"}],"time":"1364151043","postedby":"43","text":"TEST","firstname":"Name3","lastname":"Name3","picture":"806243.jpg"}]

I can run a loop on the entire array by doing this:

 $.each(data, function(i, data) { //function })

but I can't figure out how to get a loop to run on the sub array 'starers'

What's the best way of doing this?

centree
  • 2,399
  • 7
  • 28
  • 32

2 Answers2

1

Assuming your JSON really is valid, it's a regular JavaScript array:

for (var i = 0; i < thing.starers.length; i++) {
    var starer = thing.starers[i];

    ...
}
Blender
  • 289,723
  • 53
  • 439
  • 496
0

Inside your .each callback, data will be each object that happens to have a starers property. So just loop again from there (I renamed data to item for clarity):

$.each(data, function(i, item) { 
    $.each(item.starers, function(i, starer) { 
        // do something
        // e.g. alert(starer.starfirstname);
    });
});
bfavaretto
  • 71,580
  • 16
  • 111
  • 150