I'm feeling overwhelmed by this. Could you lend a hand?
Consider the following .NET classes:
Teacher:
public class Teacher
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Class[] Classes { get; set; }
}
Class
public class Class
{
public int Id { get; set; }
public string Name { get; set; }
public Attendee[] Attendees { get; set; }
}
Attendee
public class Attendee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Presences { get; set; }
}
Using this structure, let's take the following data as an illustration:
var teachers =
new Teacher[] {
new Teacher{ Id = 1, FirstName="Michael", LastName = "Knight",
Classes = new Class[]{
new Class{ Id = 1, Name = "Maths",
Attendees = new Attendee[]{
new Attendee{ Id = 1, FirstName = "Templeton", LastName = "Peck", Presences=22 },
new Attendee{ Id = 2, FirstName = "Stringfellow", LastName = "Hawke", Presences=20 }
}
},
new Class{ Id = 1, Name = "English",
Attendees = new Attendee[]{
new Attendee{ Id = 3, FirstName = "Morris", LastName = "Buttermaker", Presences=25 },
new Attendee{ Id = 4, FirstName = "Ben", LastName = "Matlock", Presences=18 },
new Attendee{ Id = 5, FirstName = "Kelly", LastName = "Taylor", Presences=22 },
new Attendee{ Id = 5, FirstName = "Marty", LastName = "McFly", Presences=0 }
}
}
}
},
new Teacher{ Id = 2, FirstName="Murray", LastName = "Bozinsky",
Classes = new Class[]{
new Class{ Id = 1, Name = "Maths",
Attendees = new Attendee[]{
new Attendee{ Id = 6, FirstName = "Luke", LastName = "Duke", Presences=21 },
new Attendee{ Id = 7, FirstName = "Bo", LastName = "Darville", Presences=26 },
new Attendee{ Id = 8, FirstName = "Frank", LastName = "Columbo", Presences=20 }
}
},
new Class{ Id = 1, Name = "English",
Attendees = new Attendee[]{
new Attendee{ Id = 9, FirstName = "Philip", LastName = "Banks", Presences=28 },
new Attendee{ Id = 10, FirstName = "Lynn", LastName = "Tanner", Presences=18 }
}
}
}
}
};
Now I want to add up all presences of each teacher's attendees. In JavaScript, using LINQ, it would look like this:
var result = teachers
.Select(p =>
new {
TeacherFirstName = p.FirstName,
TeacherLastName = p.LastName,
Presences = p.Classes
.SelectMany(q => q.Attendees.Select(r => r.Presences))
.Sum() });
This gives me (in JSON format):
[
{"TeacherFirstName":"Michael","TeacherLastName":"Knight","Presences":107},
{"TeacherFirstName":"Murray","TeacherLastName":"Bozinsky","Presences":113}
]
How could I achieve the same result in JavaScript with the provided JSON object?
[
{
"Id":1,"FirstName":"Michael","LastName":"Knight",
"Classes":
[
{
"Id":1,"Name":"Maths",
"Attendees":
[
{"Id":1,"FirstName":"Templeton","LastName":"Peck","Presences":22},
{"Id":2,"FirstName":"Stringfellow","LastName":"Hawke","Presences":20}
]
},
{
"Id":1,"Name":"English",
"Attendees":
[
{"Id":3,"FirstName":"Morris","LastName":"Buttermaker","Presences":25},
{"Id":4,"FirstName":"Ben","LastName":"Matlock","Presences":18},
{"Id":5,"FirstName":"Kelly","LastName":"Taylor","Presences":22},
{"Id":5,"FirstName":"Marty","LastName":"McFly","Presences":0}
]
}
]
},
{
"Id":2,"FirstName":"Murray","LastName":"Bozinsky",
"Classes":
[
{
"Id":1,"Name":"Maths",
"Attendees":
[
{"Id":6,"FirstName":"Luke","LastName":"Duke","Presences":21},
{"Id":7,"FirstName":"Bo","LastName":"Darville","Presences":26},
{"Id":8,"FirstName":"Frank","LastName":"Columbo","Presences":20}
]
},
{
"Id":1,"Name":"English",
"Attendees":
[
{"Id":9,"FirstName":"Philip","LastName":"Banks","Presences":28},
{"Id":10,"FirstName":"Lynn","LastName":"Tanner","Presences":18}
]
}
]
}
]
I've tried using reduce
and map
, but can't seem to get it right. Any suggestions on how to accomplish this in just one line or multiple lines will be greatly appreciated.
Thank you for your help!