Hello I'm trying to add a object to my Students array using a constructor,.
This would be my Student constructor.
var Student = function (name, address, city, state, gpa) {
this.name = name;
this.address = address;
this.city = city;
this.state = state;
this.gpa = gpa;
console.log(name, address, city, state, gpa);
};
In my main js I would call and add the objects like so.
var Student1 = [
new Student("Some Guy","Some address","some city","some state",[2.5,3.1,4.0]),
new Student("Some Guy","Some address","some city","some state",[2.5,3.1,4.0])
];
But I want to add a new object later on, I thought I could just create a Student2 and then just Student1.push(Student2); and then it would add the data to the Student1 array. But I just get undefined [object Object] when it's displaying the innerHTML.
var Student2 = [
new Student("Some Guy","Some address","some city","some state",[2.5,3.1,4.0])
];
Student1.push(Student2);
Can anyone help me get this third object into the Student1 object array?
Student1.push(Student2);you're pushing an array into an array. You should push it directly, like:Student1.push(new Student(...));.