Reputation: 307
How do I iterate through a JSON return via jQuery and return the index of each object?
Examples of some of my code below:
foreach ( x = somevalue ; x < length of array ; x +++
{
that x must be the index
[0] => Array
$('#actual_'+x).text(data.actual+" hrs.");
$('#total_'+x).text(data.total+" hrs.");
$('#regular_'+x).text(data.regular+" hrs.");
}
(
[0] => Array
(
[actual] => 9
[total] => 10
[regular] => 0
[over] => 11
[total_h] => 11
[eng_pay] => 148.5
[rate] => 9
)
[1] => Array
(
[actual] => -1
[total] => 0
[regular] => -1
[over] => 2
[total_h] => 1
[eng_pay] => 18
[rate] => 9
)
)
I would like to iterate through this jQuery ajax widget's success function:
$.ajax({
type:'POST',
url: 'cal_grid.php',
dataType: 'json',
cache: false,
data:$('#grid_frm').serialize(),
success: function(data)
{
alert(data);
} // response call back ends
});//ajax call ends
Upvotes: 0
Views: 89
Reputation: 846
untested, but should do the trick. throw this into your success function.
$.each($(data), function(i, obj){
console.log(i); //spits out your index into console
});
Upvotes: 1