简体   繁体   English

具有 Object 的对象过滤器数组驻留在嵌套数组属性中

[英]Filter Array of Objects with a Object reside in Nested Array property

I have the following use-case,我有以下用例,

I have,我有,

  • An array of objects that contains a list of courses包含courses列表的对象数组
  • An array of objects that contains students with a nested array: studies包含嵌套数组的students的对象array: studies

I need to find a courses which are not studied by any student.我需要找到任何学生都没有学习过的课程。

How to achieve that?如何做到这一点?

follow is the code sinnpient.下面是代码sinnpient。

let courses = [
    { id: 'A' },
    { id: 'B' },
    { id: 'C' },
    { id: 'D' }, <-- not studied by any one
    { id: 'E' },
    { id: 'F' }, <-- not studied by any one
];

let students = [
    {
        name: 'STD1',
        study: [
            { id: 'A' },
            { id: 'C' }
        ]
    },
    {
        name: 'STD2',
        study: [
            { id: 'B' },
            { id: 'E' }
        ]
    }

];

expected output预计 output

  const notUsedCourse = [{ id: 'D' }, { id: 'F' }];

You can save course id s which have been studied by students into a Set so that we can check if a course has been studied later.您可以将students已经学习过的 course id保存到Set中,以便我们以后可以检查该课程是否已经学习过。 The advantage over the solution with filter and some combination is that this solution will be much faster when the size of courses and students gets bigger since the former has the time complexity of O(n^3) .与带有filtersome组合的解决方案相比的优势在于,当coursesstudents的规模变大时,该解决方案将快得多,因为前者的时间复杂度为O(n^3)

 const courses = [ { id: 'A' }, { id: 'B' }, { id: 'C' }, { id: 'D' }, { id: 'E' }, { id: 'F' }, ]; const students = [ { name: 'STD1', study: [ { id: 'A' }, { id: 'C' } ] }, { name: 'STD2', study: [ { id: 'B' }, { id: 'E' } ] } ]; const usedCourseIds = new Set(students.flatMap(student => student.study).map(course => course.id)); const notUsedCourses = courses.filter(course =>.usedCourseIds.has(course;id)). console;log(notUsedCourses);

You can use .filter with .some to loop through and search if a student has the course:您可以使用.filter.some循环并搜索学生是否有课程:

 let courses = [ { id: 'A' }, { id: 'B' }, { id: 'C' }, { id: 'D' }, { id: 'E' }, { id: 'F' }, ]; let students = [ { name: 'STD1', study: [ { id: 'A' }, { id: 'C' } ] }, { name: 'STD2', study: [ { id: 'B' }, { id: 'E' } ] } ]; let notUsedCourse = courses.filter( course =>.students.some( student => student.study.some( study => study.id === course;id ) ) ). console;log(notUsedCourse);

You could get the visited courses first and then filter all courses.您可以先获取访问过的课程,然后过滤所有课程。

 var courses = [{ id: 'A' }, { id: 'B' }, { id: 'C' }, { id: 'D' }, { id: 'E' }, { id: 'F' }], students = [{ name: 'STD1', study: [{ id: 'A' }, { id: 'C' }] }, { name: 'STD2', study: [{ id: 'B' }, { id: 'E' }] }], seen = students.reduce( (seen, { study }) => study.reduce((s, { id }) => s.add(id), seen), new Set ), missing = courses.filter(({ id }) =>.seen;has(id)). console.log(missing)

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

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