1

I have the following use-case,

I have,

  • An array of objects that contains a list of courses
  • An array of objects that contains students with a nested array: studies

I need to find a courses which are not studied by any student.

How to achieve that?

follow is the code 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

  const notUsedCourse = [{ id: 'D' }, { id: 'F' }];
2
  • 3
    Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Commented Oct 22, 2019 at 9:30
  • 5
    Please first try something (use .filter(), .some(), things like that). Then, if you are really stuck, we can help you. But Stackoverflow is not a free code writing platform. We can help you but not develop the whole solution for you. Commented Oct 22, 2019 at 9:32

3 Answers 3

1

You can save course ids which have been studied by students into a Set so that we can check if a course has been studied later. 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) .

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

Sign up to request clarification or add additional context in comments.

2 Comments

Note, that .flatMap() instead of .flat().map() will perform even better.
Thanks for the advice! Though .flatMap() is equivalent to .map().flat() instead of .flat().map().
0

You can use .filter with .some to loop through and search if a student has the course:

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

3 Comments

It's an easy enough problem for an experienced programmer, and didn't seem like a "write this code for me" question. This one can be a bit tricky with the nested .some()s, which is why I chose to answer it. @JeremyThille
He posted an expected output, a clear problem statement. By all means: This fits our site.
You were silently downvoted, and my comment, in which I meant no harm and I was only wondering something, was silently censored... classic Stackoverflow.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.