Created
November 6, 2020 19:37
-
-
Save iandesj/f73c40b33c09d126a535ddaafb56f5d8 to your computer and use it in GitHub Desktop.
Exclude array items from another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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