Explanation

What you want is to fill a table (or another DOMElement) in HTML, with your JavaScript, which is executed dynamically once the page is loaded and your JSON object is received.

You want to loop through the object. The best way to do so would be with a for loop, and making sure our looping variable remains valid for the length of our object (all its attributes).

The best way to get the length of a JSON object is through myJSONObject.length: You select the keys of myJSONObject and return their count.

You can access the values stored in your JSON Object the following way, in your for loop (assuming the looping variable defined is named i): myJSONObject[i].theAttributeIWantToGet


Price formatting breakdown

Now, those prices need to have a proper format, don't they? So we'll check if any of the value attribute has less than 2 characters after the . within them. If they do, we add another decimal 0. We also add a $ before writing the formatted value. Here is a breakdown of how it works:

  • obj[i].value.toString().substring(startIndex, length)

    • We want to check the length after the . sign, so our startIndex will be the position of this dot within our string.
    • obj[i].value.toString().substring(obj[i].value.toString().indexOf('.'),length)
    • We now need to set the length. We want to find the length of all what's after the dot, so we'll take the length of the whole string just to be safe.
    • Final result: obj[i].value.toString().substring(obj[i].value.toString().indexOf('.'), obj[i].value.toString().length) < 2

      • This will return true or false. If it's true: There's less than 2 digits after the dot !
    • We add the if statement and the last zero:

    • if (obj[i].value.toString().substring(obj[i].value.toString().indexOf('.'), obj[i].value.toString().length) < 2) obj[i].value += "0";

Also: Why I use innerHTML instead of appendChild().


Solution

JSFiddle

HTML

<table>
    <tbody id="tbody"></tbody>
</table>

JSON

[{
    "key": "apple",
    "value": 1.90
}, {
    "key": "berry",
    "value": 1.7
}, {
    "key": "banana",
    "value": 1.5
}, {
    "key": "cherry",
    "value": 1.2
}]

JavaScript

Note: The JSON object will be named obj in this instance.

var tbody = document.getElementById('tbody');

for (var i = 0; i < obj.length; i++) {
    var tr = "<tr>";

    /* Verification to add the last decimal 0 */
    if (obj[i].value.toString().substring(obj[i].value.toString().indexOf('.'), obj[i].value.toString().length) < 2) 
        obj[i].value += "0";

    /* Must not forget the $ sign */
    tr += "<td>" + obj[i].key + "</td>" + "<td>$" + obj[i].value.toString() + "</td></tr>";

    /* We add the table row to the table body */
    tbody.innerHTML += tr;
}

JSFiddle

Answer from Jeff Noel on Stack Overflow
🌐
W3Schools
w3schools.com › jsref › dom_obj_table.asp
HTML DOM Table Object
The Table object represents an HTML <table> element.
Top answer
1 of 9
20

Explanation

What you want is to fill a table (or another DOMElement) in HTML, with your JavaScript, which is executed dynamically once the page is loaded and your JSON object is received.

You want to loop through the object. The best way to do so would be with a for loop, and making sure our looping variable remains valid for the length of our object (all its attributes).

The best way to get the length of a JSON object is through myJSONObject.length: You select the keys of myJSONObject and return their count.

You can access the values stored in your JSON Object the following way, in your for loop (assuming the looping variable defined is named i): myJSONObject[i].theAttributeIWantToGet


Price formatting breakdown

Now, those prices need to have a proper format, don't they? So we'll check if any of the value attribute has less than 2 characters after the . within them. If they do, we add another decimal 0. We also add a $ before writing the formatted value. Here is a breakdown of how it works:

  • obj[i].value.toString().substring(startIndex, length)

    • We want to check the length after the . sign, so our startIndex will be the position of this dot within our string.
    • obj[i].value.toString().substring(obj[i].value.toString().indexOf('.'),length)
    • We now need to set the length. We want to find the length of all what's after the dot, so we'll take the length of the whole string just to be safe.
    • Final result: obj[i].value.toString().substring(obj[i].value.toString().indexOf('.'), obj[i].value.toString().length) < 2

      • This will return true or false. If it's true: There's less than 2 digits after the dot !
    • We add the if statement and the last zero:

    • if (obj[i].value.toString().substring(obj[i].value.toString().indexOf('.'), obj[i].value.toString().length) < 2) obj[i].value += "0";

Also: Why I use innerHTML instead of appendChild().


Solution

JSFiddle

HTML

<table>
    <tbody id="tbody"></tbody>
</table>

JSON

[{
    "key": "apple",
    "value": 1.90
}, {
    "key": "berry",
    "value": 1.7
}, {
    "key": "banana",
    "value": 1.5
}, {
    "key": "cherry",
    "value": 1.2
}]

JavaScript

Note: The JSON object will be named obj in this instance.

var tbody = document.getElementById('tbody');

for (var i = 0; i < obj.length; i++) {
    var tr = "<tr>";

    /* Verification to add the last decimal 0 */
    if (obj[i].value.toString().substring(obj[i].value.toString().indexOf('.'), obj[i].value.toString().length) < 2) 
        obj[i].value += "0";

    /* Must not forget the $ sign */
    tr += "<td>" + obj[i].key + "</td>" + "<td>$" + obj[i].value.toString() + "</td></tr>";

    /* We add the table row to the table body */
    tbody.innerHTML += tr;
}

JSFiddle

2 of 9
8

It can be simply done by a small & smart process:

<table cellpadding="2" cellspacing="2" border="0" bgcolor="#dfdfdf" width="40%" align="center">
<thead>
    <tr>
        <th>Name</th>
        <th width="20%">Age</th>
        <th width="12%">Status</th>
    </tr>
</thead>
    <tbody id="tableData"></tbody>
</table>
<script type="text/javascript">
    var mainObj = [
        {
            name: "Kapil",
            age:  21,
            status: "Active"
        },
        {
            name: "John",
            age:  28,
            status: "Inactive"
        },
        {
            name: "Deos",
            age:  18,
            status: "Active"
        }
    ];
    var k = '<tbody>'
    for(i = 0;i < mainObj.length; i++){
        k+= '<tr>';
        k+= '<td>' + mainObj[i].name + '</td>';
        k+= '<td>' + mainObj[i].age + '</td>';
        k+= '<td>' + mainObj[i].status + '</td>';
        k+= '</tr>';
    }
    k+='</tbody>';
    document.getElementById('tableData').innerHTML = k;
    </script>
🌐
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
Once we have created the <table>, <tbody>, <tr>, and <td> elements, and then the text node, we then append each object to its parent in the opposite order: First, we attach each text node to its parent <td> element using ... Remember this technique. You will use it frequently in programming for the W3C DOM. First, you create elements from the top down; then you attach the children to the parents from the bottom up. Here's the HTML markup generated by the JavaScript ...
🌐
GitHub
github.com › enaqx › js-object-to-table
GitHub - enaqx/js-object-to-table: JavaScript objects drawing themselves into a table using React
JavaScript objects drawing themselves into a table using React - enaqx/js-object-to-table
Starred by 6 users
Forked by 4 users
Languages   JavaScript 100.0% | JavaScript 100.0%
🌐
GitHub
gist.github.com › gabrielcsapo › 63e41dd1620d5e41cfb662c1eb953c50
create an html table from a javascript object · GitHub
create an html table from a javascript object. GitHub Gist: instantly share code, notes, and snippets.
🌐
DocStore
docstore.mik.ua › orelly › webprog › DHTML_javascript › 0596004672_jvdhtmlckbk-chp-14-sect-8.html
14.7 Transforming JavaScript Objects into HTML Tables
You want to render embedded JavaScript data as an HTML table · When JavaScript data is formatted as an array of objects, it is a relatively simple job to use the data to drive the creation of rows and cells of an HTML table. Here is an example of some repetitive data assembled on the server ...
🌐
Valentino G.
valentinog.com › blog › html-table
Back To The Basics: How To Generate a Table With JavaScript
February 2, 2020 - In this tutorial we saw how to generate a table with JavaScript. An HTML table is represented in the DOM by the HTMLTableElement. This interface exposes a lot of useful methods for manipulating table heads with createTHead and table rows with insertRow. HTML table rows on the other hand inherit from HTMLTableRowElement. This interface has two methods, one of the most important being insertCell. Given an array of objects it is possible to iterate over them with a for...of loop for generating table rows.
🌐
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 - Example: The below code implements the innerHTML property to create an HTML table using a JavaScript array. ... <!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; } createTableWithInnerHTML(); </script> </body> </html>
Find elsewhere
🌐
Dottoro
help.dottoro.com › ljtfrumb.php
table object JavaScript
You are here: Reference > JavaScript > client-side > HTML DOM > objects > nodes and tags > table · Browser support: Specifies an element that arranges its contents into rows and columns.
Top answer
1 of 5
6

Two issues at first:

  1. Is that Name or Nome? Please fix your typo (I know Nome is French, so if it's not typo, I suggest you introduce an i18n solution).
  2. Also please mind your syntax errors (some have given suggested edits that you could consider).

After that, here's my approach for programmatically create tables based on input data:

function buildTable(labels, objects, container) {
  var table = document.createElement('table');
  var thead = document.createElement('thead');
  var tbody = document.createElement('tbody');

  var theadTr = document.createElement('tr');
  for (var i = 0; i < labels.length; i++) {
    var theadTh = document.createElement('th');
    theadTh.innerHTML = labels[i];
    theadTr.appendChild(theadTh);
  }
  thead.appendChild(theadTr);
  table.appendChild(thead);

  for (j = 0; j < objects.length; j++) {
    var tbodyTr = document.createElement('tr');
    for (k = 0; k < labels.length; k++) {
      var tbodyTd = document.createElement('td');
      tbodyTd.innerHTML = objects[j][labels[k].toLowerCase()];
      tbodyTr.appendChild(tbodyTd);
    }
    tbody.appendChild(tbodyTr);
  }
  table.appendChild(tbody);

  container.appendChild(table);
}

var labels1 = ['ID', 'Name']; 
var objects1 = [
  {"id": "1", 'name': "richard"},
  {"id": "2", 'name': "santos"}
];

var labels2 = ['ID', 'NOME'];
var objects2 = [
  {"id": "1", 'nome': "richard"},
  {"id": "2", 'nome': "adriana"}
];

var labels3 = ['ID', 'NAME', 'PLATE'];
var objects3 = [
  {"id": "1", 'name': "jetta",  'plate': "DFG-1222"},
  {"id": "2", 'name': "fusion", 'plate': "DFF-3342"}
];

buildTable(labels1, objects1, document.getElementById('a'));
buildTable(labels2, objects2, document.getElementById('b'));
buildTable(labels3, objects3, document.getElementById('c'));
table {
  border-collapse: collapse;
}
th, td {
  border: 1px solid black;
}
<div id="a"><p>Table 1</p></div>
<div id="b"><p>Table 2</p></div>
<div id="c"><p>Table 3</p></div>

2 of 5
4

This is a working procedure from our project. It has three parameters:

htmlTable(selector, data_array [, column_names]);

column_names parameter is optional: if you omit it the function creates column names from first row (if it exists).

It creates HTML tags directly into the DOM, but it can be rewritten to generate HTML as a string if you need . See the working snippet below:

var labels = ['id','name']; 
var object = [{"id":"1",'name': "richard"},{"id":"2",'name': "santos"}];
htmlTable("#res0",object, labels);

var labels = ['id','nome']; 
var object = [{"id":"1",'nome': "richard"},{"id":"2",'nome': "adriana"}];
htmlTable("#res1",object, labels);

var labels = ['id','name', 'plate']; 
var object = [{"id":"1",'name': "jetta",'plate': "DFG-1222"},
              {"id":"2",'name': "fusion",'plate': "DFF-3342"}];
htmlTable("#res2",object, labels);

// Without labels array
var data3 = [{a:1,c:2},{a:3,c:3}];
htmlTable("#res3",data3);

function htmlTable(selector, data, columns) {
	var sel = document.querySelector(selector);
	if(!sel) {
		throw new Error('Selected HTML element is not found');
	};	

	if((!columns) || columns.length == 0) {
        columns = Object.keys(data[0]);
	}

	var tbe = document.createElement('table');
	var thead = document.createElement('thead');
	tbe.appendChild(thead);

  var tre = document.createElement('tr');
  for(var i=0;i<columns.length;i++){
    var the = document.createElement('th');
    the.textContent = columns[i];
    tre.appendChild(the);
  }
  thead.appendChild(tre);

	var tbody = document.createElement('tbody');
	tbe.appendChild(tbody);
	for(var j=0;j<data.length;j++){
		var tre = document.createElement('tr');
		for(var i=0;i<columns.length;i++){
			var the = document.createElement('td');
			the.textContent = data[j][columns[i]];
			tre.appendChild(the);
		}
		tbody.appendChild(tre);
	};
	emptyDOMChildren(sel);
	sel.appendChild(tbe);
};

// Utility function to fast delete all children of element if it is not empty
// Can be replaced with simple but relatively "slower" container.innerHTML = "";
function emptyDOMChildren (container){
  var len = container.childNodes.length;
  while (len--) {
    container.removeChild(container.lastChild);
  };
};
<div id="res0"></div>
<div id="res1"></div>
<div id="res2"></div>
<div id="res3"></div>

🌐
Wtools
wtools.io › convert-js-object-to-html-table
Convert Javascript object to HTML table Online | WTOOLS
Free tool for online converting Javascript literal object into HTML table, generate HTML code from JS object quickly.
🌐
YouTube
youtube.com › watch
How to Create Table From an Array of Objects in Javascript - YouTube
In this tutorial, you will learn how to create a table from an array of objects using JavaScript. If you're new to web development or just starting out with ...
Published   March 5, 2020
🌐
Linux Hint
linuxhint.com › create-table-from-array-objects-javascript
How to Create Table From an Array of Objects in JavaScript
October 9, 2022 - In JavaScript, for the formation of a table from an array of objects, the HTML “table” string or “map()” method can be utilized. To do so, specify a div tag with an id. Then, declare the array of objects in both methods, store table tags within variables, or directly return them to ...
🌐
freeCodeCamp
forum.freecodecamp.org › t › create-a-dyinamic-table-of-object-with-js-and-show-datas › 369627
Create a dyinamic table of object with JS and show datas - The freeCodeCamp Forum
April 7, 2020 - Hi coders, In these days i’m doing Object and OOP concept in Javascript. I create this dynamic table but show me typeof of my object and not object’s properties . Then i need to create another class “Gilda” ( I still have to think what to put inside) and add in my table ( i think to ...
🌐
javaspring
javaspring.net › blog › create-html-table-from-javascript-object
How to Create an HTML Table from a JavaScript Object Array (with Auto-Incrementing ID, Name & Relevance Columns) for Beginners — javaspring.net
In this tutorial, we’ll walk through creating a dynamic HTML table from a JavaScript object array. We’ll include **auto-incrementing IDs** (to uniquely identify each row), **Name**, and **Relevance** columns. By the end, you’ll understand how to dynamically generate tables, manipulate ...
🌐
npm
npmjs.com › package › object-to-table
object-to-table - npm
January 9, 2021 - Object-to-Table is a very small JavaScript library, providing the capability to render an object into a HTML (or otherwise marked up) table.
      » npm install object-to-table
    
Published   Jan 09, 2021
Version   0.9.2
Author   Dr. Ralf S. Engelschall
🌐
GitHub
gist.github.com › 4151867
JavaScript: HTML table to object · GitHub
JavaScript: HTML table to object. GitHub Gist: instantly share code, notes, and snippets.
Top answer
1 of 3
7

Let's start with your version:

1) Unnecessary optimization.

var array_length = Object.keys(basic_data).length;
var array_obj_names = Object.keys(basic_data);

This gives you nothing in terms of performance. It would be optimized away by the browser.

2) Chain-calling

$(table_id).empty();
$(table_id).append('<thead>');

It is possible to do chain-calls like: $('#elem').doA().doB().doC()

3) Misunderstanding calls

$(table_id).find('thead:last').append('<th>Tag');

.append() does exactly that: appending after the last element.

4) NuStyleLoopz

  for (var i = 0; i < array_length; i++) {
    var attr_name = array_obj_names[i];
    var tag       = '<td>' + array_obj_names[i];
    var data      = '<td>' + basic_data[attr_name];

You could iterate over the keys directly via Object.keys() with

  • Array.prototype.forEach()

  • Array.prototype.map()

  • Array.protoype.reduce() / Array.prototype.reduceRight().

Here a slighty pimped version:

makeTag=function(openTag, closeTag){
            return function(content){
                return openTag+content+closeTag;  
            };
        };
tHead=makeTag("<thead>","</thead>");
tBody=makeTag("<tbody>","</tbody>");
td=makeTag("<td>","</td>");
tr=makeTag("<tr>","</tr>");

function insertBasicTable(data,id){
    $('#'+id).html(
        tHead(
            tr(
                td("Tag")+
                td("Data")
            )
        )+
        tBody(
            Object.keys(data).reduce(function(o,n){
                return o+tr(
                    td(n)+""+
                    td(data[n]+"")
                );
            },"")
        )
    );
};

insertBasicTable({"key1":"value1","key2":"value2"},"mytable");

Here is the fiddle to play with.

To make HTML-generation less painfull and a bit more JS-stylish I define what is called a higher order function makeTag: simply a function returning a function. A call to var tHead=makeTag("<thead>","</thead>"); generates an anonymous function (a function without name), which returns every time it's called "<thead>"+ parameter +"</thead>". The same goes for the other tags. This allows me to do something like a DSL:

    tHead(
        tr(
            td("Tag")+
            td("Data")
        )
    )

That is not only handy, but cool B-) Besides it is nearly as readable as html and not so noisy like the typical string concatenation and the risk of typos is really low.

2 of 3
1

You should cache $table and $tbody, instead of re-instantiating them every time you need them.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › console › table_static
console: table() static method - Web APIs | MDN
The console.table() static method displays tabular data as a table. ... The data to display. This must be either an array or an object. Each item in the array, or property in the object, is represented by a row in the table.