-3
var array = [
['name1', 'material1', 'number1', 'place1', 'country1'],
['name2', 'material2', 'number2', 'place2', 'country2']
];

This is the js array

<table>
    <tr>
        <th>Name</th>
        <th>Material</th>
        <th>Number</th>
        <th>Place</th>
        <th>Country</th>
    </tr>
</table>

This is the html table i have

Now i want the values of the array generated in my table with pure JS. How is this possible?

7
  • Have you searched or tried anything so far? do you have any JS knowledge? Do some effort and show us where you struggle and we will help for sure. Commented Mar 22, 2019 at 9:59
  • Yes i have worked with JS before, only i have no idea how to work this out. @ZakariaAcharki Commented Mar 22, 2019 at 10:01
  • refer this w3schools.com/jsref/met_table_insertrow.asp Commented Mar 22, 2019 at 10:01
  • Please do add your progress so far in your post to address a specific issue and not just ask for a given desired result from a given value. Commented Mar 22, 2019 at 10:02
  • 1
    Possible duplicate of Generate HTML table from 2D JavaScript array and Javascript create table with for loop and array Commented Mar 22, 2019 at 10:03

2 Answers 2

1

Check this fiddler: https://jsfiddle.net/a3r8ze0g/

You need to itinerate the array like this:

arr.forEach(function(elem){ //Where arr is the array

var arr = [
['name1', 'material1', 'number1', 'place1', 'country1'],
['name2', 'material2', 'number2', 'place2', 'country2']
];

var tabla = document.getElementById('newTable');

arr.forEach(function(elem){

    var tr = tabla.insertRow();
  tr.innerHTML = '<td>'+elem[0]+'</td><td>'+elem[1]+'</td><td>'+elem[2]+'</td><td>'+elem[3]+'</td><td>'+elem[4]+'</td>';
})

var arr = [
['name1', 'material1', 'number1', 'place1', 'country1'],
['name2', 'material2', 'number2', 'place2', 'country2']
];

var tabla = document.getElementById('newTable');

arr.forEach(function(elem){
  
	var tr = tabla.insertRow();
  tr.innerHTML = '<td>'+elem[0]+'</td><td>'+elem[1]+'</td><td>'+elem[2]+'</td><td>'+elem[3]+'</td><td>'+elem[4]+'</td>';
})
<table id="newTable">
    <tr>
        <th>Name</th>
        <th>Material</th>
        <th>Number</th>
        <th>Place</th>
        <th>Country</th>
    </tr>
</table>

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

Comments

0

You can loop over to the array and create a HTML string with <tr> and <td> then apply those to the inner HTML of the table element:

var array = [
['name1', 'material1', 'number1', 'place1', 'country1'],
['name2', 'material2', 'number2', 'place2', 'country2']
];
var nHTML = document.querySelector('table').innerHTML;
array.forEach(function(arrItem){
  var td = '<tr>';
  arrItem.forEach(function(item){
    td += '<td>'+item+'</td>' ;
  });
  nHTML += td + '</tr>';
});
document.querySelector('table').innerHTML = nHTML;
<table border='1'>
    <tr>
        <th>Name</th>
        <th>Material</th>
        <th>Number</th>
        <th>Place</th>
        <th>Country</th>
    </tr>
</table>

2 Comments

Works perfect, THANKS!
@JackKiurre you can tick mark the answer if it was helpful to you

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.