Take the numbering off the property names, eg name1 should just be name etc Then make a function like below:
function modifyStudent(studentObj,name,address,city,state,gpa){
studentObj.name=name;
studentObj.address = {
address:address,
city:city,
state:state
};
studentObj.gpa = gpa;
}
then call the function with the student object and the arguments for name address etc
var studentInfo = [{
name:"Test",
address:{
address:"No where",
city:"Chicago",
state:"IL",
},
gpa:[]
}];
modifyStudent(studentInfo[0],'super man', '123 Test Dr', 'Orlando', 'Florida', [3.2, 4.0, 2.2]);
console.log(studentInfo[0]);
Since javascript is pass by reference for objects and arrays, when the function modify's the objects properties those changes will be visible when the studentInfo[0]
object is used after it.
Or you could make a student object that has a method to modify the student data
function student(){
this.name="";
this.address={address:"",city:"",state:""};
this.gpa=[];
this.modifyStudent(name,address,city,state,gpa){
this.name=name;
this.address = {
address:address,
city:city,
state:state
};
this.gpa = gpa;
};
}
var studentInfo= [new student(),new student()];
console.log(studentInfo[0]);
studentInfo[0].modifyStudent('super man', '123 Test Dr', 'Orlando', 'Florida', [3.2, 4.0, 2.2]);