Home > OS >  Finds value of property
Finds value of property

Time:11-11

I am pretty new to Javascript. I am trying to get the sum of the grades of the 3 student properties i have declared. The problem is that i dont understand why it says undefined on my "grade" value. Also if there is any other easier way to find the values of the grades of each student other than writing it like:

classroom.student1.grade classroom.student2.grade classroom.student3.grade

Thank you in advance!

let classroom = {
numberOfChairs: 20,
numberofTables: 10,
student1: [
    {fullName: "", 
    age: "", 
    grade:8,
    electronics: {
        brand:"", 
        model:"",
        batteryCapacity:""
    }}
],
student2: [
    {fullName: "", 
    age: "", 
    grade:8,
    electronics: {
        brand:"", 
        model:"",
        batteryCapacity:""
    }}
],
student3:[
    {fullName: "", 
    age: "", 
    grade: 8,
    electronics: {
        brand:"", 
        model:"",
        batteryCapacity:""
    }}
],
teachers: [
    {fullName:"", yearsOfExperience:"", salary:""}
],
};

function getGrades() {
    grades = classroom.student1.grade   classroom.student2.grade   classroom.student3.grade 
        console.log(grades)
}

getGrades();

`

I am trying to find out why it says that my value of the students is "undefined" and what i shall write differently for it to find the value.

CodePudding user response:

classroom.student1.grade

Look at your data structure. classroom is an object. student1 is a property on that object, and that property contains an array. Arrays have no property called grade. So the value is undefined.

Either access the element in the array:

classroom.student1[0].grade

Or make it an object instead of an array of objects:

student1: {
  fullName: "", 
  age: "", 
  grade:8,
  electronics: {
    brand:"", 
    model:"",
    batteryCapacity:""
  }
}

I'm guessing these shouldn't be arrays in the first place, based on the names and structures.


Unless you want an array of students. For example:

students: [
  {
    /* properties from student1 */
  },
  {
    /* properties from student2 */
  },
  {
    /* properties from student3 */
  }
]

Then you can have as many "students" as you like. And getting the sum would be a reduce() operation. For example:

classroom.students.reduce((prev, curr) => prev.grade   curr.grade, 0)

(Though it's not clear to me why anyone would want to sum the grades of all the students, but it's your data/logic so it's up to you.)

CodePudding user response:

Because students are arrays each with one item, I don't think it's a good structure, If you ensure that you have only one item, you should refer the property directly to the object, then it will work fine.

let classroom = {
numberOfChairs: 20,
numberofTables: 10,
student1: {fullName: "", 
    age: "", 
    grade:8,
    electronics: {
        brand:"", 
        model:"",
        batteryCapacity:""
    }},
student2:
    {fullName: "", 
    age: "", 
    grade:8,
    electronics: {
        brand:"", 
        model:"",
        batteryCapacity:""
    }},
student3:
    {fullName: "", 
    age: "", 
    grade: 8,
    electronics: {
        brand:"", 
        model:"",
        batteryCapacity:""
    }},
teachers: [
    {fullName:"", yearsOfExperience:"", salary:""}
],
};

Anyway, with this structure as the target object is first item in the array, you need to access the zero index.

classroom.student1[0].grade   classroom.student2][0].grade   classroom.student3[0].grade
  • Related