William Pfaffe William Pfaffe - 1 year ago 114
Javascript Question

Add object from array to table

I've been trying to get around, howto add my Student object, to my array. Once I got that fixed, I tried to display what's inside the array, onto a table, in a html file.

Student Object:

var Student = function (fullName, email, phone, category, groupID) {
this.fullName = fullName;
this.email = email;
this.phone = phone;
this.category = category;
this.groupID = groupID;


var studentArray = new Array(Student);

makeTable function:

function makeTable() {
var student1 = new Student("Waw","waaw","awaw","waaw","waaw");
document.write("<thead><tr><th>Full Name</th><th>Email</th><th>Phone</th><th>Category</th><th>Group</th></tr></thead>");
for(i = 0; i < studentArray.length; i++){
document.write("<tr><td>" + studentArray[i].fullName +"</td><td>" + studentArray[i].email +"</td><td>" + studentArray[i].phone +"</td><td>" + studentArray[i].category +"</td><td>" + studentArray[i].groupID +"</td></tr>");

Html File:

<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../script/scripts.js" type="text/javascript"></script>




I regulary program in Java normally, however, only reason I could imagine this not working, is as if I made the array the wrong way, or my array still is empty, after inserting student1 into studentArray.

Thanks in advance.

Answer Source

javascript is a weakly typed language (more about language sthrength)

whith this line you are not defining 'Stundent' as a type for your array but insead you push an object named Student (which does not exist in you context) into your array 'studentArray'. meaning you push undefined or null into stundenArray.

var studentArray = new Array(Student);

if you change it to this you wil create an empty array

var studentArray = new Array();

and then it should work

because some browsers will throw an exception on the following line because the compiler doesn't know about a property on the object pointing to null

document.write("<tr><td>" + studentArray[i].fullName +"</td><td>" + studentArray[i].email +"</td><td>" + studentArray[i].phone +"</td><td>" + studentArray[i].category +"</td><td>" + studentArray[i].groupID +"</td></tr>");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download