简体   繁体   English

尝试比较 Javascript 中不同数组中的对象

[英]Trying to compare objects in different arrays in Javascript

数组结构

Hi, I'm building an app that shows students which courses they qualify for based on their grades.嗨,我正在构建一个应用程序,它会根据学生的成绩向学生显示他们有资格参加哪些课程。 I have to filter courses that have the same subjects as the student's subjects and the mark must be >= value .我必须过滤与学生科目相同科目的课程,并且mark必须是 >= value I've attached an image of how the JSON looks like, the first 6 objects are the student's marks, the other 10 objects are the courses, each course has an array of subjects... these are the subjects we're checking.我附上了一张关于 JSON 的图片,前 6 个对象是学生的分数,其他 10 个对象是课程,每门课程都有一系列主题......这些是我们正在检查的主题。

For the student array, you can create a mark_lookup Map which points each Subject key to a Mark value.对于student数组,您可以创建一个mark_lookup Map ,将每个Subject键指向一个Mark值。 Then, you can use .filter() on your courses array to find all courses where .every() subject from the inner Subjects array has a mark_lookup value greater than or equal to the value for that particular subject.然后,你可以使用.filter()你对courses阵列找到所有的课程哪里.every()从内部主体Subjects数组有一个mark_lookup值大于或等于该特定主题的价值。

See example below:请参阅下面的示例:

 const student = [{Subject: "math", Mark: 75}, {Subject: "science", Mark: 45}, {Subject: "programming", Mark: 90}]; const courses = [{ Name: "Computer Science", Subjects: [{name: "math", value: 50}, {name: "programming", value: 60}] }, { Name: "Information Technology", Subjects: [{name: "programming", value: 50}] }, { Name: "Chemistry", Subjects: [{name: "math", value: 50}, {name: "science", value: 50}] }, { Name: "Psychology", Subjects: [{name: "english", value: 50}, {name: "science", value: 50}] }]; const getCourses = (student, courses) => { const mark_lookup = new Map(student.map(({Subject, Mark}) => [Subject, Mark])); return courses.filter(({Subjects}) => Subjects.every( ({name, value}) => (mark_lookup.get(name) || 0) >= value )); } console.log(getCourses(student, courses));
 .as-console-wrapper { max-height: 100% !important;} /* ignore */

1) Create student marks object from student array. 1)从学生数组创建学生marks对象。 2) Go thru course, and each course filter the subjects bases on the requirement. 2)通过课程,每门课程根据要求筛选科目。 3) push course with updated subjects to results if subjects are more than 0 3) 如果科目数大于 0,则将更新科目的课程推向结果

 const student = [ { Subject: "English First Additional Language", Mark: 80 }, { Subject: "Physical Sciences", Mark: 60 }, { Subject: "Mathematics", Mark: 70 } ]; const courses = [ { Name: "Computer Science", Subjects: [ { name: "English First Additional Language", value: 50 }, { name: "Mathematics", value: 80 } ] }, { Name: "Information Technology", Subjects: [{ name: "Physical Sciences", value: 50 }] }, { Name: "Chemistry", Subjects: [{ name: "Physical Sciences", value: 70 }] } ]; const marks = student.reduce((acc, { Subject, Mark }) => { acc[Subject] = Mark; return acc; }, {}); const short_listed = courses.reduce((acc, { Name, Subjects }) => { const sel_subjects = Subjects.filter(({ name, value }) => marks[name] > value); if (sel_subjects.length > 0) { acc.push({ Name, Subjects: sel_subjects }); } return acc; }, []); console.log(short_listed);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM