I have a project I am working, one part of the project requires it to use an array of objects. I understand what an array is and also understand how to create an object. I created a new object named student with properties of name, grade and student ID. each one of these object properties will be generated by user in input text fields. I have a button that I use to save every entry into an array but do not understand how to incorporate my student object into my array or not understanding properly what an array of objects is which is requirement for this project. I already had it set up to work with a parallel array and commented out lines to add my new student object and new student ID input field but now I am stuck. I researched it and am not getting information needed to complete this task or it is not very clear. This is what I have so far
<br>
<p><b>Student Name:</b></p>
<input id="inp" type="text"><br><br>
<p><b>Grade:</b></p>
<input id="inps" type="text"><br><br>
<p><b>Student ID:</b></p>
<input id="inpsid" type="text"><br><br>
<button type="button" onclick="enter()">Enter</button><br><br>
<button type="button" onclick="construct()">Constructor</button>
<p>Student Names List:</p>
<p id="iop"></p><br>
<p id="opo"></p><br>
<p id="hop"></p><br>
<p id="lop"></p><br>
<p id="aop"></p><br>
<p id="cop"></p><br>
<script>
var studentArr = new Array();
var scoreArr = new Array();
function enter() {
var student = {
name: document.getElementById("inp").value,
grade : "A",
id : 001
};
// Just to test my object property is saving input correctly.
document.getElementById("iop").innerHTML = student.name;
studentArr.push(student.name);
var stuval = "";
for (i=0; i < studentArr.length; i++) {
stuval = stuval + studentArr[i] + "<br/>";
}
}
</script>
Change studentArr.push(student.name); to studentArr.push(student);. Right now you are only storing student names. You have created a student object with name, grade, and id - all bundled together. You can store the entire object in the array and read it later with studentArr[Array Index].property Where the property is either name, grade, or id.