2016-04-03 37 views
-3
 "Pascal" : [ 
       {"Name": "Pascal made simple","price":"700"}, 
       {"Name": "Guide to pascal","price":"400"}, 
       {"Name": "Pascal for all","price":"500"}], 


     "Scala" : [ 
       {"Name":"Scala for Dummies","price":"1000"}, 
       {"Name":"Scala in Depth","Price":"1300"}] 

請幫我寫一個for-loop在JavaScript中以顯示Pascal書籍作爲HTML表格。用於在Javascript中循環顯示書籍;

+2

請張貼任何代碼顯示你已經試過什麼解決了這個問題。您可能還想查看其他[5000個類似的問題](http://stackoverflow.com/search?q=javascript+json+table)以獲取想法和答案。 – Roberto

回答

0

就遍歷所有的書籍,並將其添加爲html:

function booksToTable(books) { 
    var html = ['<table>']; 
    for (var i = 0, l = books.length; i < l; i++) { 
     var book = books[i]; 
     html.push('<tr><td>' + book.Name + '</td><td>' + book.price + '</td></tr>'); 
    } 
    html.push('</table>'); 
    return html.join('\n'); 
} 

document.write(booksToTable(input.Pascal));