简体   繁体   中英

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;
};

studentArray:

var studentArray = new Array(Student);

makeTable function:

function makeTable() {
    var student1 = new Student("Waw","waaw","awaw","waaw","waaw");
    studentArray.push(student1);
    document.write("<table>");
    document.write("<thead><tr><th>Full Name</th><th>Email</th><th>Phone</th><th>Category</th><th>Group</th></tr></thead>");
    document.write("<tbody>");
    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>");
    }
   document.write("</tbody>");
   document.write("</table>"); 
}

Html File:

<html>
    <head>
        <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>

    </head>
    <body>
        <script>
            makeTable();
            </script>
    </body>
</html>

Output:

http://i.imgur.com/K9UWIyx.png

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.

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>");

You are pushing the Student function into the array as well, you do not need to do that. It'll end up returning undefined values.

Simply replace

var studentArray = new Array(Student);

with

var studentArray = new Array();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM