0

I have two observables one is a list of available courses and one is a list of coursework completed. My task is it create a list of courses that can be taken next. The problem is when I do a filter I can find where the compare is true easy enough but I can't figure out how to get the inverse list.

Here is my current function

public CourseList(): Observable < ICourse[] > {
  return this.CourseworkServ.Read()
    .pipe(
      switchMap((CurrentCourses: IResponseObject < ICourseworkData > ) => {
        return this.CourseServ.Read(this.SelectedDepartment.Id)
          .pipe(
            map((CourseList: IResponseObject < ICourse > ) => CourseList.Array.filter((CheckCourse: ICourse) => CurrentCourses.Array.some(({
              Id,
              Edition,
              Institution,
              Department,
              Course
            }) => CheckCourse.Id === Id)))
          )
      })
    )
}  

This function will show courses taken from the list of courses, what I need is a list of courses not taken. But if I can't just use the !== in the Id compare because the result would never be true. I need something like an inverse filter or something like that. Does anyone have any suggestions on how to fix this?

Manish Balodia
  • 1,863
  • 2
  • 23
  • 37
J. Kramer
  • 63
  • 7
  • This is nothing to do with observables, surely; you have two arrays and want to identify the items in one that do not have a match in the other. – jonrsharpe May 27 '19 at 20:51
  • 1
    What about `! CurrentCourses.Array.some(({Id, Edition, Institution, Department, Course}) => CheckCourse.Id === Id)`? [`Array.prototype.some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) returns `true` if the passed predicate is true for at least one element (in your case if the course was taken). So, the inverse is exactly what you ask for. – FK82 May 27 '19 at 21:42
  • @jonrsharpe Yes, the problem isn't directly related to `observables` but the asynchronous state of the data does limit the solutions (at least the ones I came up with). @FK82 that would be perfect, I didn't think that the result array would track the negative set. I'll try it though, if it works that is exactly what I need so thank you. – J. Kramer May 28 '19 at 15:20

0 Answers0