You could iterate the array and build a new row if the remainder with 5 is zero.
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
tr;
array.forEach((v, i) => {
var td = document.createElement('td');
if (!(i % 5)) {
tr = document.createElement('tr');
document.getElementById('table0').appendChild(tr);
}
td.appendChild(document.createTextNode(v));
tr.appendChild(td);
});
<table id="table0"></table>
Answer from Nina Scholz on Stack OverflowMedium
medium.com โบ wdstack โบ quick-blurb-generating-a-table-from-an-array-in-javascript-41386fd449a9
Quick Blurb: Generating a Table from an Array in JavaScript | by Zak Frisch | WDstack | Medium
January 24, 2017 - //setup our table array var tableArr = [ ["row 1, cell 1", "row 1, cell 2"], ["row 2, cell 1", "row 2, cell 2"] ]//create a Table Object let table = document.createElement('table');//iterate over every array(row) within tableArr for (let row of tableArr) {//Insert a new row element into the table element table.insertRow();//Iterate over every index(cell) in each array(row) for (let cell of row) {//While iterating over the index(cell) //insert a cell into the table element let newCell = table.rows[table.rows.length - 1].insertCell();//add text to the created cell element newCell.textContent = cell; } }//append the compiled table to the DOM document.body.appendChild(table);
Codedec
codedec.com โบ tutorials โบ how-to-create-dynamic-table-from-array-in-javascript
How To Create dynamic table from Array in JavaScript โ CODEDEC
The innerHTML property is a property in JavaScript that allows the script to manipulates an HTML page by reading and writing data within a HTML tag. ... <!doctype html> <html> <head> <title>Create dynamic table from ArrayList using JavaScript</title> <style> .input{ border:none; font-size:10px; } .input:focus{ outline:none; } </style> </head> <body> <h2 style="text-align:left">Create dynamic table from ArrayList using JavaScript</h2> <table id="fetch" border="2" cellspacing="1" cellpadding="8" class="table"> <tr> <th>A</th> <th>B</th> <th>C</th> </tr> <script type="text/javascript"> var array
Create dynamic html table using javascript from simple array - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I want to write some JavaScript to create a simple HTML table from an array that just contains numbers: ... But the JavaScript code should be dynamic depending on the arrays size (always a factor ... More on stackoverflow.com
javascript - jquery dynamically create table from array - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Closed 4 years ago. I am currently trying to create a table from an array. More on stackoverflow.com
Generate HTML table from 2D JavaScript array - Stack Overflow
In JavaScript, is it possible to generate an HTML table from a 2D array? The syntax for writing HTML tables tends to be very verbose, so I want to generate an HTML table from a 2D JavaScript array,... More on stackoverflow.com
javascript - How to create table dynamically according to data in an array - Stack Overflow
var arr=["1","2","-3","4","-5","-6","7"] // data changes as well arr[] size ; Here, scenario is, it is to me made in table More on stackoverflow.com
Videos
05:33
JSON Array to HTML Table with Javascript - YouTube
08:23
How to Create Table From an Array of Objects in Javascript - YouTube
12:29
How To Populate HTML Table From Array Using Javascript [ with Source ...
12:12
Display array of objects using #javaScript and #HTML | populate ...
08:58
How to convert an HTML table to a JavaScript array - YouTube
GeeksforGeeks
geeksforgeeks.org โบ javascript โบ how-to-create-an-html-table-from-an-object-array-using-javascript
How to Create an HTML Table from an Object Array Using JavaScript ? - GeeksforGeeks
July 23, 2025 - <!DOCTYPE html> <html lang="en"> <body> <script> const data = [ { name: 'Rahul', age: 25, city: 'New Delhi' }, { name: 'Vijay', age: 30, city: 'Muzaffarpur' }, { name: 'Gaurav', age: 22, city: 'Noida' }, ]; function createTableWithInnerHTML() { let tableHTML = '<table border="1"><tr>'; Object.keys(data[0]).forEach(key => { tableHTML += `<th>${key}</th>`; }); tableHTML += '</tr>'; data.forEach(item => { tableHTML += '<tr>'; Object.values(item).forEach(value => { tableHTML += `<td>${value}</td>`; }); tableHTML += '</tr>'; }); tableHTML += '</table>'; document.body.innerHTML += tableHTML; } creat
Top answer 1 of 3
9
You could iterate the array and build a new row if the remainder with 5 is zero.
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
tr;
array.forEach((v, i) => {
var td = document.createElement('td');
if (!(i % 5)) {
tr = document.createElement('tr');
document.getElementById('table0').appendChild(tr);
}
td.appendChild(document.createTextNode(v));
tr.appendChild(td);
});
<table id="table0"></table>
2 of 3
1
var array = [1,2,3,4,5,6,7,8,9,10];
var result = "<table border=1>";
result += "<tr>";
for (var j = 0; j < array.length; j++) {
result += "<td>" + array[j] + "</td>";
if ((j + 1) % 5 == 0) {
result += "</tr><tr>";
}
}
result += "</tr>";
result += "</table>";
document.body.innerHTML = result;
Please give it a try if that works
Delft Stack
delftstack.com โบ home โบ howto โบ javascript โบ javascript create table dynamically
How to Create Table Dynamically in JavaScript | Delft Stack
February 2, 2024 - var table = document.createElement('table'); var tr = document.createElement('tr'); var arrheader = ['ID', 'FirstName', 'LastName', 'Gender']; var array = [ {ID: 1, FirstName: 'Mehvish', LastName: 'Ashiq', Gender: 'Female'}, {ID: 2, FirstName: 'Thomas', LastName: 'Christoper', Gender: 'Male'}, {ID: 3, FirstName: 'Shiva', LastName: 'Pandy', Gender: 'Male'}, {ID: 4, FirstName: 'Tina', LastName: 'Robert', Gender: 'Female'} ]; for (var j = 0; j < array.length; j++) { var th = document.createElement('th'); // column var text = document.createTextNode(arrheader[j]); // cell th.appendChild(text); tr.
Stack Overflow
stackoverflow.com โบ questions โบ 68514044 โบ jquery-dynamically-create-table-from-array
javascript - jquery dynamically create table from array - Stack Overflow
const exampleArray = [ { header: 'ID', values: [1, 2] }, { header: 'First Name', values: ['John', 'Jayne'] }, { header: 'Last Name', values: ['Doe', 'Doe'] } ]; const header = $('#results thead tr'); const body = $('#results tbody'); exampleArray.forEach((column) => { header.append( $(`<th>${column.header}</th>`) ); }); // Compute the number of rows in the array const nRows = exampleArray[0].values.length; for (let i = 0; i < nRows; i++) { // row contains all cells in a row let row = $("<tr/>"); // Loop over the columns exampleArray.forEach((column) => { // For each column, we add the i-th value, since we're building the i-th row row.append(`<td>${column.values[i]}</td>`); }); // Add the row to the table body body.append(row); }
Codepedia
codepedia.info โบ create-dynamic-html-table-using-jquery
Create Dynamic HTML Table from Array data [JQUERY] - Codepedia
August 24, 2021 - jQuery Add Dynamic Table with data in Array: Here in this article will explain how to create HTML table dynamically from the Array variable using jQuery. This means the columns, rows, and data get added dynamically to HTML table at runtime using jQuery. You can also check the previous article related jQuery table like How to remove table row tr in jQuery , Remove next row tr from a table in jQuery.
Top answer 1 of 16
88
Here's a function that will use the dom instead of string concatenation.
function createTable(tableData) {
var table = document.createElement('table');
var tableBody = document.createElement('tbody');
tableData.forEach(function(rowData) {
var row = document.createElement('tr');
rowData.forEach(function(cellData) {
var cell = document.createElement('td');
cell.appendChild(document.createTextNode(cellData));
row.appendChild(cell);
});
tableBody.appendChild(row);
});
table.appendChild(tableBody);
document.body.appendChild(table);
}
createTable([["row 1, cell 1", "row 1, cell 2"], ["row 2, cell 1", "row 2, cell 2"]]);
2 of 16
46
This is pretty easy to do with a double for loop.
function makeTableHTML(myArray) {
var result = "<table border=1>";
for(var i=0; i<myArray.length; i++) {
result += "<tr>";
for(var j=0; j<myArray[i].length; j++){
result += "<td>"+myArray[i][j]+"</td>";
}
result += "</tr>";
}
result += "</table>";
return result;
}
Top answer 1 of 3
1
var arr = ["1","2","-3","4","-5","-6","7"];
var table = document.createElement("table"), // create the table element
tbody = document.createElement("tbody"); // create the table body element
table.appendChild(tbody); // add the table body to table
// ### POSITIVE ROW ######################
var tr = document.createElement("tr"); // create the table row element
arr.forEach(function(e) { // for each element e in arr
var td = document.createElement("td"); // create a table cell element
if(e >= 0) { // if e is positive
td.textContent = e; // then change the text content of this cell to e
td.style.background = "#ccc"; // and change the background as well
}
tr.appendChild(td); // add the cell to the row (if the number is negative the cell will be empty and without background)
});
tbody.appendChild(tr); // add the row to table
//### NEGATIVE ROW ######################
tr = document.createElement("tr"); // same but for negative numbers ...
arr.forEach(function(e) {
var td = document.createElement("td");
if(e < 0) {
td.textContent = e;
td.style.background = "#ccc";
}
tr.appendChild(td);
});
tbody.appendChild(tr);
document.body.appendChild(table);
You can group some of the code in a function and reuse it:
// elt is shortcut for document.createElement
function elt(tag) {
return document.createElement(tag);
}
// take an array and a condition function conditionFN and return a row of all the element that passed the condition function with a background of #ccc
function makeRow(arr, conditionFN) {
var tr = elt("tr");
arr.forEach(function(e) {
var td = elt("td");
if (conditionFN(e)) {
td.textContent = e;
td.style.background = "#ccc";
}
tr.appendChild(td);
});
return tr;
}
// take an array and return the equivalent table element
function makeTable(arr) {
var table = elt("table"),
tbody = elt("tbody");
table.appendChild(tbody);
tbody.appendChild(makeRow(arr, function(e) { return e >= 0; })); // the condition function only allows positive numbers => positive row
tbody.appendChild(makeRow(arr, function(e) { return e < 0; })); // the condition function only allows negative numbers => negative row
return table;
}
document.body.appendChild(makeTable([1, 2, -3, 4, -5, -6, -7, 8, 9, 10]));
2 of 3
0
Using recursive function and table.inertRow() and row.insertCell()
var data = ["1", "2", "-3", "4", "-5", "-6", "7"];
var table = document.getElementById('myTable')
function plusMinusRows(arr, rowNum) {
var row = table.insertRow();
arr.map(Number).forEach(function(num) {
var value = !rowNum ? (num >= 0 ? num : '') : (num < 0 ? num : '');
var cell = row.insertCell();
cell.textContent = value;
cell.style.background = value !== '' ? '#cccccc' : '';
});
// call function again when rowNum not defined
!rowNum && plusMinusRows(arr, 2);
}
// initialize 2 rows
plusMinusRows(data);
<table id="myTable"></table>
Stack Overflow
stackoverflow.com โบ questions โบ 50589783 โบ creating-table-with-javascript-and-dynamic-variables
arrays - Creating table with Javascript and dynamic variables - Stack Overflow
I am trying to make an application where you register a set of numbers, and then those numbers will be displayed in the table. I am able to register the first set of numbers, but if I change the numbers and press register again, i won't get a new row. Instead it updates the same row. Basically I am trying to make this program work in the way that pressing the register button will create a new row. window.onload = oppdater; function oppdater() { document.getElementById("btnRegistrer").onclick = registrer; } function registrer() { var arrayListe = []; var reg = {}; var verdi0 = parseInt(document
Stack Overflow
stackoverflow.com โบ questions โบ 67340810 โบ dynamically-create-html-table-from-an-array
javascript - Dynamically create html table from an Array - Stack Overflow
<template> <table> <thead> <tr> <th v-for="(column, index) in columns" :key="index"> {{ column }} </th> </tr> </thead> <tbody> <tr v-for="(row, index) in rows" :key="index"> <td v-for="(column, index) in columns" :key="index"> {{ row[column] }} </td> </tr> </tbody> </table> </template> <script> export default { name: "Table", props: { columns: Array, rows: Array, }, }; </script>
Medium
medium.com โบ @securitytalent โบ javascript-dynamically-table-generates-748b835311fc
JavaScript Dynamically Table Generates | by MD Mehedi Hasan | Medium
February 24, 2024 - When you open this HTML file in a web browser, it will dynamically generate a table with the data from the `arr` array, where each inner array represents a row of the table, and each element of the inner array represents a cell of that row. ... <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>javascript</title> </head> <body> <script> let arr = [ ['Mehedi',1,2,3], ['Hasan',4,5,6], ['jony',7,8,9], ] document.write("<table border='1px'>") for(var i = 0; i < arr.length; i++) { document.write('<tr>') for(var j = 0; j < arr[i].length; j++) { document.write("<td>" + arr[i][j] + " </td>"); } document.write('</tr>') } document.write("</table>") </script> </body> </html>
MDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ API โบ Document_Object_Model โบ Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces
Traversing an HTML table with JavaScript and DOM Interfaces
You can build this table and its internal child elements by using just a few DOM methods. Remember to keep in mind the tree model for the structures you are planning to create; this will make it easier to write the necessary code. In the <table> tree of Figure 1 the element <table> has one ...
Jsplain
jsplain.com โบ javascript โบ index.php โบ Thread โบ 78-Creating-an-HTML-table-with-dynamic-items-from-an-array
Creating an HTML table with dynamic items from an array - Practicing AngularJS: support material - JavaScript in Plain Language
October 22, 2014 - On line 20 an ng-repeat directive is introduced with a statement calling all the elements of array employees to be passed in via the temporary variable person. To know more about ng-repeat please refer to chapter 2.2 of the eBook. Line 20 forms a TR ( table row), which means that ng-repeat will take the row as template in order to instantiate TDs ( table data columns).
Top answer 1 of 5
5
You could get first the headers of the table and while iterating the column names, you could collect the length of every data array and take the max length of it for later iterating the td elements.
var data = [{ Header: "Column1", Values: ["75", "79", "83"] }, { Header: "Column2", Values: ["77", "81", "86", "90"] }, { Header: "Column3", Values: ["98", "117"] }],
table = document.createElement('table'),
tr = document.createElement('tr'),
rows = [],
max = 0,
i;
table.appendChild(tr);
data.forEach(function (o) {
var th = document.createElement('th');
th.appendChild(document.createTextNode(o.Header));
tr.appendChild(th);
max = Math.max(max, o.Values.length);
});
for (i = 0; i < max; i++) {
tr = document.createElement('tr'),
table.appendChild(tr);
data.forEach(function (o) {
var td = document.createElement('td');
tr.appendChild(td);
td.appendChild(document.createTextNode(i in o.Values ? o.Values[i] : ''));
});
}
document.body.appendChild(table);
2 of 5
1
You'll have to use DOM methods and nested for loops to achieve this.
var tableJSON = [
{
"Header": "Column1",
"Values": [
"75",
"79",
"83"
]
},
{
"Header": "Column2",
"Values": [
"77",
"81",
"86",
"90"
]
},
{
"Header": "Column3",
"Values": [
"98",
"117"
]
}
];
var tableDiv = document.createElement("table");
var trElement = document.createElement("tr");
tableDiv.appendChild( trElement );
var prDiv = document.getElementById("pr");
tableJSON.forEach(function(a_column, index){
if(a_column.Header)
{
var tdElement = document.createElement("td");
tdElement.innerHTML = "<b>" + a_column.Header + "</b>";
trElement.appendChild( tdElement );
}
if(a_column.Values){
var allRows = tableDiv.childNodes;
for(var i=0 ;i< a_column.Values.length; i++)
{
var rowWanted = allRows[i+1];
if( !rowWanted )
{
rowWanted = document.createElement("tr");
tableDiv.appendChild( rowWanted );
}
if(rowWanted.childNodes.length==0)
for(var j=0; j< tableJSON.length; j++){
rowWanted.appendChild( document.createElement("td") );
}
rowWanted.childNodes[ index ].innerHTML = a_column.Values[i];
}
}
});
prDiv.appendChild(tableDiv);
<div id="pr">
</div>