What exactly did we do here?
The key is in the three
for-each statements that we used. One loops through the array of table header strings and appends a table cell (td, or th for header cells) element with each value to the thead element's row. Then there are two nested .forEach statements that do the same for each row in the body. We technically only have one row in the body right now, so we probably didn't need that messy double for-each, but now all we have to do to add another row to the table is simply append another data array to the rows variable. We'll talk a bunch more about Array.prototype.forEach and other array functions in the next chapter.
This might seem like a lot of work for such a simple table, but the advantages of doing it ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access