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 } ];
Capitalize values.
We are interested in retrieving only the name of the students and all the names should be in caps. e.g: ['JOHN', 'BABA', 'YAGA', 'WICK']
let names = studentRecords.map(({name}) => name.toUpperCase());
console.log(names);
Conditions for AOO.
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.
e.g: [{name: 'John', id: 123, marks : 98 },{name: 'Wick', id: 115, marks : 75 }].
e.g: [{name: 'John', id: 123, marks : 98 },{name: 'Wick', id: 115, marks : 75 }].
const newArr = studentRecords.map(({name, id, marks}) => {
if(marks > 50){
console.log({name, id, marks});
}
});
Multiple conditions for AOO.
Retrieve the details of students who scored more than 50 marks and have id greater than 120.
const newArr = studentRecords.map(({name, id, marks}) => {
if(marks > 50 && id > 120){
console.log({name, id, marks});
}
});
Summing up values.
Consider the same scenario we have discussed above, but this time we would like to know the sum total of the marks of all students.
var sum = 0;
let markSum = studentRecords.forEach(({marks}) => {
sum = sum + marks;
return sum;
});
console.log(sum);
More than 50.
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 names = studentRecords.map(({name, marks}) =>{
if(marks > 50){
console.log({name})
}
});
Condition on AOO.
This time we are required to print the sum of marks of the students with id > 120.
var sum = 0;
let markSum = studentRecords.forEach(({marks, id}) => {
if(id >= 120){
sum = sum + marks;
return sum;
}
});
console.log(sum);
Passed with grace.
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.
var sum = 0;
var newmarks = 0;
let markSum = studentRecords.forEach(({marks, id}) => {
newmarks = marks + 15;
if(newmarks >= 50){
sum = sum + newmarks;
return sum;
}
});
console.log(sum-30);
Create Array with Objects.
Create 6 objects , each object will have name, class, roll no as properties. Store these objects in an array of objects.
let student2 = {"name":"stu2", "class":"1st", "rollNo":"12"};
let student3 = {"name":"stu3", "class":"2nd", "rollNo":"21"};
let student4 = {"name":"stu4", "class":"2nd", "rollNo":"22"};
let student5 = {"name":"stu5", "class":"3rd", "rollNo":"31"};
const students = [{"name":"stu1", "class":"1st", "rollNo":"11"}];
students.push(student2, student3, student4, student5);
students.push({"name":"stu6", "class":"4th", "rollNo":"41"})
console.log(students)