从一个带有JavaScript的对象获得带有唯一对(键:值)的新对象
我有一些列出教师、科目和学生的数据。我想创建一个包含与教师关联的主题的表,其中不得有任何重复的对,但如果关联的对不相同,则可能存在重复的教师或重复的主题。这将是数据:
const data =
[
{
"teacher": "John Doe",
"subject": "Maths",
"student": "student1"
},
{
"teacher": "John Doe",
"subject": "Maths",
"student": "student2"
},
{
"teacher": "John Doe",
"subject": "Art",
"student": "student1"
},
{
"teacher": "Robert Wilson",
"subject": "History",
"student": "student1"
},
{
"teacher": "John Doe",
"subject": "Music",
"student": "student1"
},
{
"teacher": "Mark Smith",
"subject": "Maths",
"student": "student2"
},
{
"teacher": "Mary Martin",
"subject": "Science",
"student": "student1"
},
{
"teacher": "Mary Martin",
"subject": "Spanish",
"student": "student1"
},
{
"teacher": "Paula Wilson",
"subject": "Band",
"student": "student1"
},
{
"teacher": "Mark Smith",
"subject": "Band",
"student": "student2"
}
]
我尝试使用以下代码:
var subjectDict = {}
data.forEach (x => {
subjectDict [x.subject] = x.teacher
})
var teacherRow = Object.values (subjectDict)
var subjects = Object.keys (subjectDict)
但是输出不准确,因为它只给我每个科目一次,即使它是由两位不同的老师教授的(乐队或数学应该与不同的老师相关联出现两次)。
我用我尝试过的东西创建了一个jsfiddle:
https://jsfiddle.net/soleuil/ f2r70Lte/1/
解决方案应该给我一个表,其中第一行应填充第一个元素,最后一行应填充第二个元素(考虑到顺序可以不同):
First Row : Second Row
"Maths":"John Doe"
"Art":"John Doe"
"History":"Robert Wilson"
"Music":"John Doe"
"Maths":"Mark Smith"
"Science":"Mary Martin"
"Spanish": "Mary Martin"
"Band":"Paula Wilson"
"Band":"Mark Smith"
提前谢谢您。
I have some data listing teachers, subjects and students. I want to create a table with subjects associated with teachers where there must not be any repeating pair, but there might be a repeated teacher or repeated subject if the associated pair is not the same. This would be the data:
const data =
[
{
"teacher": "John Doe",
"subject": "Maths",
"student": "student1"
},
{
"teacher": "John Doe",
"subject": "Maths",
"student": "student2"
},
{
"teacher": "John Doe",
"subject": "Art",
"student": "student1"
},
{
"teacher": "Robert Wilson",
"subject": "History",
"student": "student1"
},
{
"teacher": "John Doe",
"subject": "Music",
"student": "student1"
},
{
"teacher": "Mark Smith",
"subject": "Maths",
"student": "student2"
},
{
"teacher": "Mary Martin",
"subject": "Science",
"student": "student1"
},
{
"teacher": "Mary Martin",
"subject": "Spanish",
"student": "student1"
},
{
"teacher": "Paula Wilson",
"subject": "Band",
"student": "student1"
},
{
"teacher": "Mark Smith",
"subject": "Band",
"student": "student2"
}
]
I have tried using the following code:
var subjectDict = {}
data.forEach (x => {
subjectDict [x.subject] = x.teacher
})
var teacherRow = Object.values (subjectDict)
var subjects = Object.keys (subjectDict)
However the output is not accurate because it gives me each subject just once even though it is taught by two different teachers (Band or Maths should appear twice associated with different teachers).
I have created a jsfiddle with the thing that I have tried:
https://jsfiddle.net/soleuil/f2r70Lte/1/
The solution should give me a table where the first row should be filled with the first element, and the last row should be filled with the second element (considering that the order can be different):
First Row : Second Row
"Maths":"John Doe"
"Art":"John Doe"
"History":"Robert Wilson"
"Music":"John Doe"
"Maths":"Mark Smith"
"Science":"Mary Martin"
"Spanish": "Mary Martin"
"Band":"Paula Wilson"
"Band":"Mark Smith"
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将为您提供主题,并带有一系列关联教师的钥匙:
This will give you the subject as a key with an array of associated teachers with the output:
根据您所需的输出,我会稍微修改您的代码以返回一系列对象。
Based on your desired output, I would slightly modify your code to return an array of objects.
使用 :
Using
Array#reduce
: