-1

I want to make a table from an array. I want to have 3 rows and 3 columns. The rows heading name should be 1,2,3. And the columns should be the name of the arrays: "country", "Capital", "population".

Here is my html:

<!DOCTYPE html>
<html>
<head>
</head>
<body>    
    <script>        
    var country = ["Norway", "Sweden", "Denmark"];
    var capital = ["Oslo", "Stockholm", "Copenhagen"];
    var population = ["5,2", "9.8", "5,7"];    

</body>
</html>
6
  • 2
    Possible duplicate of Print out Javascript array in table Commented May 29, 2017 at 2:05
  • Are you using jQuery or Javascript? Commented May 29, 2017 at 2:10
  • 1
    You asked for this here ??? Commented May 29, 2017 at 5:35
  • Possible duplicate of Javascript array to html table Commented May 29, 2017 at 7:18
  • This is not a question this is a Request - SO is not and never will be a free coding service. Commented May 29, 2017 at 7:21

2 Answers 2

1

Here is an example of how to do it using jQuery. Per the comment of Roko C. Buljan I have changed the append to a concatenation.

//Array
var country = ["Norway", "Sweden", "Denmark"];
var capital = ["Oslo", "Stockholm", "Copenhagen"];
var population = ["5,2", "9.8", "5,7"];

//Start the table
var table = '<table>';

//Add the Headers
table += '<thead><tr><th>Country</th><th>Capital</th><th>Population</th></tr></thead>';

//Start the body
table += '<tbody>';

//Add the inner rows
for (i = 0; i < country.length; i++) {
  table += '<tr><td>' + country[i] + '</td><td>' + capital[i] + '</td><td>' + population[i] + '</td></tr>';

}

//Close the body and the table
table += '</tbody></table>';

//Add the completed table to the HTML
$('#table').append(table);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='table'></div>

Sign up to request clarification or add additional context in comments.

4 Comments

I know the OP explicitly asked for three rows and three columns, but still, wouldn't it be better if the loop was based on the array length rather than hardcoding it as < 3?
@nnnnnn Yup, changed it.
Don't - append inside for loops. Concatenate-only instead.
@RokoC.Buljan Changed it. I have actually run into a problem with appending inside for loops before, but it seemed to work fine in this situation.
0

Nest the arrays into an object. From there attach to the table by repeating cells.

var country = ["Norway","Sweden", "Denmark"];
var capital = ["Oslo", "Stockholm", "Copenhagen"];
var population = ["5,2", "9.8", "5,7"];

var table={};
country.forEach((countries,key)=>
table[key]={country:countries,capital:capital[key],population:population[key]})
function tableCreate( table,l=Object.keys(table).length){
  var count=1;
 var body = document.body,
    tbl  = document.createElement('table');
    tbl.style.width  = '100px';
    tbl.style.border = '1px solid black';
    for(var i in table){
        var tr = tbl.insertRow();

 tr.appendChild(document.createTextNode(count));
      for(var j in table[i]){
           td = tr.insertCell(); 
          td.appendChild(document.createTextNode(table[i][j]));
          td.style.border = '1px solid black';     
        }
      count++
    }
    body.appendChild(tbl);
}
tableCreate(table);

or

var country = ["Norway","Sweden", "Denmark"];
var capital = ["Oslo", "Stockholm", "Copenhagen"];
var population = ["5,2", "9.8", "5,7"];

var table={};
country.forEach((countries,key)=>
table[key]={country:countries,capital:capital[key],population:population[key]})
function tableCreate( table,l=Object.keys(table).length){
 var body = document.body,
    tbl  = document.createElement('table');
    tbl.style.width  = '100px';
    tbl.style.border = '1px solid black';
    for(var i in table){
        var tr = tbl.insertRow();
        for(var j in table[i]){
            var td = tr.insertCell();
          td.appendChild(document.createTextNode(table[i][j]));
          td.style.border = '1px solid black';     
        }
    }
    body.appendChild(tbl);
}
tableCreate(table);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.