gpt4 book ai didi

javascript - 如何在 JavaScript 中以类似于 LINQ 的方式进行分组和求和

转载 作者:行者123 更新时间:2023-11-28 12:11:48 24 4
gpt4 key购买 nike

我对此感到疯狂。也许你可以帮忙。

假设有以下 .NET 类:

老师:

public class Teacher
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Class[] Classes { get; set; }
}

类别

public class Class
{
public int Id { get; set; }
public string Name { get; set; }
public Attendee[] Attendees { get; set; }
}

与会者

public class Attendee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Presences { get; set; }
}

根据这个结构我们以如下数据为例:

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 }
}
}
}
}
};

我现在想做的是总结所有出席的教师。在 LINQ 中我会这样做:

var result = teachers
.Select(p =>
new {
TeacherFirstName = p.FirstName,
TeacherLastName = p.LastName,
Presences = p.Classes
.SelectMany(q => q.Attendees.Select(r => r.Presences))
.Sum() });

这给了我(作为 JSON):

[
{"TeacherFirstName":"Michael","TeacherLastName":"Knight","Presences":107},
{"TeacherFirstName":"Murray","TeacherLastName":"Bozinsky","Presences":113}
]

如何使用 JavaScript 获取以下 JSON 对象来执行此操作:

[
{
"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}
]
}
]
}
]

我正在玩reducemap。但我就是无法让它发挥作用。您是否有提示,如何在一个(或两个,或三个,...)衬里中完成此操作?

提前谢谢您。

最佳答案

LINQ对于 Javascript,您可以使用几乎相同的代码来获得求和结果。

var teachers = [{ 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 }] }] }],
result = Enumerable.From(teachers)
.Select(p => ({
TeacherFirstName: p.FirstName,
TeacherLastName: p.LastName,
Presences: Enumerable.From(p.Classes)
.SelectMany(q => Enumerable.From(q.Attendees).Select('$.Presences'))
.Sum()
}))
.ToArray();

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>

关于javascript - 如何在 JavaScript 中以类似于 LINQ 的方式进行分组和求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60613617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com