I need help with a JavaScript task. How do I get something specific queried from two databases?
Task: View the index.js file Implement the gradeOverview() function, which gets the variables students and grades and creates a grade overview for each student. Thereby each element in the students array should be projected to an object in the following format: { student: (students[i]), grades: [(grades[j], grades[j+k], ...)] }
This is my function which accesses both databases and should retrieve and display a value from one database at a time based on the "student number".
function gradeOverview(students, grades) {
const result = students.map((student) => [
{
student: student,
grade: grades.reduce((grades, grade) => {
const student number = grade.studentnumber;
if (grades[matriculationnumber] == null) grades[matriculationnumber] = [];
grades[matriculationnumber].push(grade);
return grades;
}),
},
]);
console.log(result);
return result;
// TODO: implement me
}
The Data:
var students = [{
"matrikelnummer": 4636,
"vorname": "Vérane",
"nachname": "Voase"
}]
var grades = [{
"id": 628,
"matrikelnummer": 4636,
"grade": "3,3"
},
{
"id": 886,
"matrikelnummer": 4636,
"grade": "5,0"
}]
Output:
"student": {
"matrikelnummer": 4636,
"vorname": "Vérane",
"nachname": "Voase"
},
"grades": [
{
"id": 628,
"matrikelnummer": 4636,
"grade": "3,3"
},
{
"id": 886,
"matrikelnummer": 4636
"grade": "3,6"
}
]
},
Updated: Fiddle Link https://jsfiddle.net/uzrodex3/
I hope i guessed correctly your incoming data you put into gradeOverview function.
const studentsQueryResponse = [
{
name: "John Doe",
studentNumber: 123
},
{
name: "Johan Doe",
studentNumber: 321
},
{
name: "Jane Doe",
studentNumber: 111
}
]
const gradesQueryResponse = [
{
studentNumber: 123,
grade: 1
},
{
studentNumber: 123,
grade: 3
},
{
studentNumber: 321,
grade: 5
},
{
studentNumber: 123,
grade: 1
}
]
There is your function which returns in this case
[
{
"student": {
"name":"John Doe",
"studentNumber":123
},
"grade":[1,3,1]
},
{
"student": {
"name":"Johan Doe",
"studentNumber":321
},
"grade":[5]
},
{
"student": {
"name":"Jane Doe",
"studentNumber":111
},
"grade":[]
}
]
If you want to return whole grade object in array, then just remove map line which returns grade property value from grade object
function gradeOverview(students, grades) {
console.log("Input arguments", students, grades)
const results = students.map((student) => {
return {
student: student,
grade: grades
.filter((gradeObject) => gradeObject.studentNumber === student.studentNumber)
.map((gradeObject) => gradeObject.grade)
}
});
console.log("Results", results);
return results;
}
And then you call your function
gradeOverview(studenstQueryResponse, gradesQueryResponse)