You have given:
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 } ];
Que.1 We are interested in retrieving only the students' names; all the names should be in caps.
['JOHN', 'BABA', 'YAGA', 'WICK']
let studentNames = studentRecords.map(student => student.name.toUpperCase()); console.log(studentNames); // ['JOHN', 'BABA', 'YAGA', 'WICK']
Que.2 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 }]
let highScorers = studentRecords.filter((student) => student.marks > 50); console.log(highScorers); // [{name: 'John', id: 123, marks: 98 },{name: 'Wick', id: 115, marks: 75 }]
Que.3 Retrieve the details of students who scored more than 50 marks and have IDs greater than 120.
let filteredStudents = studentRecords.filter( (student) => student.marks > 50 && student.id > 120 ); console.log(filteredStudents); // [{name: 'John', id: 123, marks: 98 }]
Que.4 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.
let totalMarks = studentRecords.reduce( (total, student) => total + student.marks, 0 ); console.log(totalMarks); // 241
Que.5 This time we want to get only the names of the students who scored more than 50 marks from the same dataset used above.
let highScorersNames = studentRecords.filter((student) => student.marks > 50).map((student) => student.name); console.log(highScorersNames); // ['John', 'Wick']
Que.6 This time we are required to print the sum of marks of the students with id > 120.
let totalMarksIdGreaterThan120 = studentRecords.filter((student) => student.id > 120).reduce((total, student) => total + student.marks, 0); console.log(totalMarksIdGreaterThan120); // 98 + 45 = 143
Que.7 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.
let updatedMarks = studentRecords.map((student) => { if (student.marks < 50) { student.marks += 15; } return student; // updatedMarks = [ // { name: "John", id: 123, marks: 98 }, // { name: "Baba", id: 101, marks: 38 }, // { name: "yaga", id: 200, marks: 60 }, // { name: "Wick", id: 115, marks: 75 }, // ]; }); let highScorersWithUpdatedMarks = updatedMarks.filter((student) => student.marks > 50); // highScorersWithUpdatedMarks = [ // { name: "John", id: 123, marks: 98 }, // { name: "yaga", id: 200, marks: 60 }, // { name: "Wick", id: 115, marks: 75 }, // ]; let totalMarksAfterGrace = highScorersWithUpdatedMarks.reduce((total, student) => total + student.marks, 0); // totalMarksAfterGrace = 98 + 60 + 75 = 233 console.log(totalMarksAfterGrace); // 233
Que.8 Create 6 objects , each object will have name, class, roll no as properties. Store these objects in an array of objects.
let students = [ { name: "John", class: "A", rollNo: 1 }, { name: "Baba", class: "B", rollNo: 2 }, { name: "Yaga", class: "A", rollNo: 3 }, { name: "Wick", class: "B", rollNo: 4 }, { name: "Alice", class: "A", rollNo: 5 }, { name: "Bob", class: "B", rollNo: 6 }, ]; console.log(students); // [{name: 'John', class: 'A', rollNo: 1 }, {name: 'Baba', class: 'B', rollNo: 2 }, {name: 'Yaga', class: 'A', rollNo: 3 }, {name: 'Wick', class: 'B', rollNo: 4 }, {name: 'Alice', class: 'A', rollNo: 5 }, {name: 'Bob', class: 'B