How to create a table from an array using javascript

I am trying to figure out how I can transfer an array with multiple variables into an HTML table using only javascript. My javascript code so far is rendering everything onto the page and I don't want the tags like and such showing. The result should look something like this [table_example_image].

The instructions are:

Using JavaScript, iterate through the items in the array, and dynamically display the collection within a table under the section of your webpage.

The entire table (including replacing any true/false text with actual images like in the example) and content should be generated and outputted with JavaScript.

Within the table, create a element for each of the properties in the array collection (for example title, author, alreadyRead) - column count and text should be dynamically generated based on JavaScript, and not "hardcoded".

Create and append a for each value Use JavaScript to append and create the style rules for the table within the section of the HTML document.

Any tips would be appreciated!

    var books = [
        title: 'The Stranger',
        author: 'Albert Camus',
        alreadyRead: true
        title: 'Binging with Babish',
        author: 'Andrew Rea',
        alreadyRead: true
        title: 'You Suck at Cooking: The Absurdly Practical Guide to Sucking Slightly Less at Making Food: A Cookbook',
        author: 'You Suck at Cooking',
        alreadyRead: false


function createTable() {
    var table = document.createElement("table");  //makes a table element for the page

    table += "<tr class='firstRow'><th>Title</th><th>Author</th><th>Read?</th></tr>";  //adds the first row that contains the sections for the table

    for (var i = 0; i < books.length; i++)  //loops through the array 
        //add info from the array into this
        table += "<tr><td>" + books[i].title + "</td><td>";


Read more here:

Content Attribution

This content was originally published by Darwin Watterson at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: