从一个带有JavaScript的对象获得带有唯一对(键:值)的新对象

发布于 2025-01-20 20:11:30 字数 1952 浏览 3 评论 0原文

我有一些列出教师、科目和学生的数据。我想创建一个包含与教师关联的主题的表,其中不得有任何重复的对,但如果关联的对不相同,则可能存在重复的教师或重复的主题。这将是数据:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木槿暧夏七纪年 2025-01-27 20:11:31

这将为您提供主题,并带有一系列关联教师的钥匙:

{
  "Maths": [ "John Doe", "Mark Smith" ],
  "Art": [ "John Doe" ],
  "History": [ "Robert Wilson" ],
  "Music": [ "John Doe" ],
  "Science": [ "Mary Martin" ],
  "Spanish": [ "Mary Martin" ],
  "Band": [ "Paula Wilson", "Mark Smith" ]
}

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

let subject_teachers = data.reduce((b, a) => {
  if (!b[a.subject]) b[a.subject] = [];
  if (b[a.subject].indexOf(a.teacher) == -1) b[a.subject].push(a.teacher);
  return b;
}, {})
console.log(subject_teachers)

This will give you the subject as a key with an array of associated teachers with the output:

{
  "Maths": [ "John Doe", "Mark Smith" ],
  "Art": [ "John Doe" ],
  "History": [ "Robert Wilson" ],
  "Music": [ "John Doe" ],
  "Science": [ "Mary Martin" ],
  "Spanish": [ "Mary Martin" ],
  "Band": [ "Paula Wilson", "Mark Smith" ]
}

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

let subject_teachers = data.reduce((b, a) => {
  if (!b[a.subject]) b[a.subject] = [];
  if (b[a.subject].indexOf(a.teacher) == -1) b[a.subject].push(a.teacher);
  return b;
}, {})
console.log(subject_teachers)

李白 2025-01-27 20:11:31

根据您所需的输出,我会稍微修改您的代码以返回一系列对象。

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.map (function(x) {
       temp = {};
       temp[x.subject] = x.teacher;
       return temp;
    });
    console.log(subjectDict);


    

Based on your desired output, I would slightly modify your code to return an array of objects.

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.map (function(x) {
       temp = {};
       temp[x.subject] = x.teacher;
       return temp;
    });
    console.log(subjectDict);


    

擦肩而过的背影 2025-01-27 20:11:30

使用

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

const [subjects, teachers] = data.reduce(([subjects, teachers], { subject, teacher }) => {
  const subjectIndex = subjects.indexOf(subject);
  const exists = subjectIndex >= 0 && teachers[subjectIndex] === teacher;
  return exists 
    ? [subjects, teachers] 
    : [ [...subjects, subject], [...teachers, teacher] ];
}, [ [], [] ]);

console.log(subjects);
console.log(teachers);

Using Array#reduce:

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

const [subjects, teachers] = data.reduce(([subjects, teachers], { subject, teacher }) => {
  const subjectIndex = subjects.indexOf(subject);
  const exists = subjectIndex >= 0 && teachers[subjectIndex] === teacher;
  return exists 
    ? [subjects, teachers] 
    : [ [...subjects, subject], [...teachers, teacher] ];
}, [ [], [] ]);

console.log(subjects);
console.log(teachers);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文