I want to select from array courses only the courses that have at least one field with id in fieldIds array. bellow is the format of thw arrays:
courses = [
{id: 5, name:"some name1", fields:[{id: 28, name:"field name"}, {id: 29, name:"field name2"}]},
{id: 18, name:"some name2", fields:[{id: 11, name:"field name"}, {id: 28, name:"field name2"}]},
{id: 7, name:"some name3", fields:[{id: 29, name:"field name"}, {id: 9, name:"field name2"}]}
]
fieldIds = [29, 9]
So the result should be:
courses = [
{id: 5, name:"some name1", fields:[{id: 28, name:"field name"}, {id: 29, name:"field name2"}]},
{id: 7, name:"some name3", fields:[{id: 29, name:"field name"}, {id: 9, name:"field name2"}]}
]
This is what I tried and I always got the same array it changes nothing
fieldIds.map(fieldId => {
courses = courses.filter(course => course.fields.map(f => f.id == fieldId);
});
EDIT after trying the response of this question as follow:
fieldIds.map(fieldId => {
courses = courses.filter(course => course.fields.some(f => f.id == fielded))
}
if the fieldIds contain a couple of IDs for example the function returns courses belong to all fields. (and I want to extract the courses that have at least one of the selected fields)
fieldIds.map(...)Whycourse.fields.map(...)?