简体   繁体   中英

how can i access array element inside an object and print them all in a bootstrap table?

在此处输入图片说明

i want access the three underlined field in in the obj and and print them in in bootstarp table . now i am just getting one only .

  var len = data.hits.length;

                            console.log(len);  // len is 10


                            for (var i = 0 ; i <= len ; i ++) 

                            {


                        console.log(data.hits[i].recipe.label);
                        console.log(data.hits[i].recipe.image);
                        console.log(data.hits[i].recipe.url);


                        document.getElementById("result").innerHTML  = data.hits[i].recipe.label  ;
                        document.getElementById("result2").innerHTML = 

                        "<img src= "+ data.hits[i].recipe.image +">";


                        document.getElementById("result3").innerHTML = data.hits[i].recipe.url;




                            }

my current situation.

在此处输入图片说明

You need to append data to a element. But in your code you have replaced the element on every loop. You can append table row in following ways.

 var len = data.hits.length; console.log(len); // len is 10 var tableHtml = ""; for (var i = 0 ; i <= len ; i ++) console.log(data.hits[i].recipe.label); console.log(data.hits[i].recipe.image); console.log(data.hits[i].recipe.url); tableHtml += "<tr>"; tableHtml += "<td>" + data.hits[i].recipe.label +"</td>" ; tableHtml += "<td><img src= "+ data.hits[i].recipe.image +"></td>"; tableHtml+ = "<td>" + data.hits[i].recipe.url + "</td>"; tableHtml += "<tr>"; } document.getElementById("table").innerHTML = tableHtml; 
 <table id='table'> <table> 

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