用于在Javascript中循环显示书籍;

时间:2016-04-03 20:51:46

标签: javascript json

        "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"}]

请帮我在javascript中编写for循环,将Pascal的书籍显示为HTML表格。

1 个答案:

答案 0 :(得分: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));