Loop on object with JQuery each

1

I'm looping an object with JQuery each and I'm not succeeding.

Object generated below via PHP:

{"status":"hasP",
 "flag":null,
 "qtProcess":null,
 "code": null,
 "message": [{"id":"1","email":"[email protected]"},{"id":"2","email":"[email protected]"}]}

JQuery code to catch obejto and generate a table (part where it generates a failure):

$.each(data, function(index, value){                    
    table +='<tr><td>' + value.message.id + '</td>';
    table +='<td>' + value.message.email + '</td></tr>';
});

Generate the following error:

TypeError: value.message is undefined
    
asked by anonymous 10.02.2017 / 19:12

2 answers

2

You are not reading the object correctly, the array is in the message attribute:

var table = "";

var data =  {"status":"hasP","flag":null,"qtProcess":null,"code":null,"message":[{"id":"1","email":"[email protected]"},{"id":"2","email":"[email protected]"}]}

$.each(data.message, function(index, value){
  table +='<tr><td>' + value.id + '</td>';
  table +='<td>' + value.email + '</td></tr>';
});

console.log(table);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
10.02.2017 / 19:30
0

Accesses the message object directly into the data object with pure javascript.

for (index in data.message) {
        table +='<tr><td>' + index.id + '</td>';
        table +='<td>' + index.email + '</td></tr>';
}
    
10.02.2017 / 19:32