Skip to content

Instantly share code, notes, and snippets.

@iandesj
Created November 6, 2020 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iandesj/f73c40b33c09d126a535ddaafb56f5d8 to your computer and use it in GitHub Desktop.
Save iandesj/f73c40b33c09d126a535ddaafb56f5d8 to your computer and use it in GitHub Desktop.
Exclude array items from another
const allClasses = [
{
id: 1,
name: 'Yoga 100',
enrolled: 10,
maxCapacity: 11,
},
{
id: 2,
name: 'Yoga 200',
enrolled: 11,
maxCapacity: 11,
},
{
id: 3,
name: 'Yoga 300',
enrolled: 8,
maxCapacity: 11,
},
{
id: 4,
name: 'Yoga 400',
enrolled: 1,
maxCapacity: 11,
},
{
id: 5,
name: 'Yoga 500',
enrolled: 10,
maxCapacity: 11,
},
];
const userEnrolledClasses = [
{
id: 34,
name: 'Yoga 300',
classId: 3,
},
{
id: 58,
name: 'Yoga 500',
classId: 5,
},
];
// Get just the id's of the classes the user is enrolled in
const userEnrolledClassIds = userEnrolledClasses.map(cls => cls.classId);
// Filter out all classes that the user is already enrolled in
const availableClasses = allClasses.filter(cls => !userEnrolledClassIds.includes(cls.id))
// This will print all classes the user is not enrolled in
console.log(availableClasses);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment