简体   繁体   中英

how Object function for each

no result data, how can I get data from one of the objects in the names array by supplying the name? and class info for list query and show.

 $.when(Data('')).then(function(result) { var results = result.d.results; var html = ""; html = "<table>"; $.each(results, function(key, fila) { console.log("Data ", fila); //no result html += "<tr>"; html += "<td>" + lack name + "</td><td>" + lack occupation + "</td>"; html += "</tr>"; }); html += "</table>"; $(".info").html(html); }); function Data() { //how to get object name var person = { name: "Tahir Akhtar", occupation: "Software Development" }; var p1 = "name"; var p2 = "occupation"; alert(person[p1]); alert(person[p2]); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="info"></div> 

You need to return the object from the function. The object it returns will then be the result in the .then() callback function. You can then loop through the properties with $.each() .

 $.when(Data()).then(function(result) { var html = ""; html = "<table>"; var headerRow = ""; var dataRow = ""; $.each(result, function(key, fila) { console.log("Data ", fila); //no result headerRow += "<th>" + key + "</th>"; dataRow += "<td>" + fila + "</td>"; }); html += "<tr>" + headerRow + "</tr>"; html += "<tr>" + dataRow + "</tr>"; html += "</table>"; $(".info").html(html); }); function Data() { //how to get object name var person = { name: "Tahir Akhtar", occupation: "Software Development" }; var p1 = "name"; var p2 = "occupation"; alert(person[p1]); alert(person[p2]); return person; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="info"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM