Array of Object - Handson


                let studentRecords = [ {name: 'John', id: 123, marks : 98 },
                {name: 'Baba', id: 101, marks : 23 },
                {name: 'yaga', id: 200, marks : 45 },
                {name: 'Wick', id: 115, marks : 75 } ]
            

Q1 :- We are interested in retrieving only the students' names; all the names should be in caps.
['JOHN', 'BABA', 'YAGA', 'WICK']

ANS:-

                studentRecords.map((item)=>{
                    console.log(item.name.toUpperCase())
                })
            

ANS :- ['JOHN', 'BABA', 'YAGA', 'WICK']


Q2 :- Suppose we have the same dataset as above but this time we want to get the details of students who scored more than 50 marks.
[{name: 'John', id: 123, marks: 98 },{name: 'Wick', id: 115, marks: 75 }]

ANS :-

                studentRecords.map((item)=>{
                    if(item.marks > 50){
                        console.log(item)
                    }
                })
            

OUTPUT :- {name: 'John', id: 123, marks: 98}
{name: 'Wick', id: 115, marks: 75}


Q3 :- Retrieve the details of students who scored more than 50 marks and have IDs greater than 120.

ANS :-

                studentRecords.map((item) => {
                    if (item.marks > 50 && item.id > 120) {
                      console.log(item);
                    }
                  });
            

OUTPUT :- { name: 'John', id: 123, marks: 98 }


Q4 :- Consider the same scenario we have discussed above, but this time we would like to know the sum total of the marks of the students.

ANS :-

                const sum = studentRecords.reduce((total, current) => {
                    return total + current.marks;
                    
                }, 0);
                  console.log(sum);
            

OUTPUT :- 241


Q5 :- This time we want to get only the names of the students who scored more than 50 marks from the same dataset used above.

ANS :-

                studentRecords.map((item) => {
                    if (item.marks > 50) {
                      console.log(item.name);
                    }
                  });
            

OUTPUT :- John
Wick


Q6 :- This time we are required to print the sum of marks of the students with id > 120.

ANS :-

                const sum2 = studentRecords.filter((item) => {
                    return item.id > 120;
                }).reduce((total, current) => {
                    return total + current.marks;
                }, 0);
                console.log(sum2);
            

OUTPUT :- 143


Q7 :- This time we are required to print the total marks of the students with marks greater than 50 after a grace of 15 marks has been added to those students who scored less than 50.

ANS :-

                function calculateTotalMarksWithGrace(student) {
                    if (student.marks < 50) {
                      // Add 15 grace marks if the student's marks are less than 50
                      return student.marks + 15;
                    } else {
                      return student.marks;
                    }
                  }
                  
                  // Calculate and print total marks for students with marks greater than 50 after applying grace
                  for (let i = 0; i < studentRecords.length; i++) {
                    let student = studentRecords[i];
                    let totalMarks = calculateTotalMarksWithGrace(student);
                  
                    if (totalMarks > 50) {
                      console.log(`Student: ${student.name}, Total Marks: ${totalMarks}`);
                    }
                  }
            

OUTPUT :-
Student: John, Total Marks: 98
Student: yaga, Total Marks: 60
Student: Wick, Total Marks: 75


Q8 :- Create 6 objects , each object will have name, class, roll no as properties. Store these objects in an array of objects.

ANS :-

                let student = [
                { name: 'John', class: '10th', rollNo: 1 },
                { name: 'Jane', class: '9th', rollNo: 2 },
                { name: 'Alice', class: '12th', rollNo: 3 },
                { name: 'Bob', class: '11th', rollNo: 4 },
                { name: 'Sarah', class: '8th', rollNo: 5 },
                { name: 'Mike', class: '10th', rollNo: 6 }
                ];
                console.log(student);