I have arrays containing tasks and tags:
const tasks = [
{id: 0, name: 'a', tags: [{id: 0, name: 'q'}, {id: 7, name: 'i'}]},
{id: 1, name: 'b', tags: [{id: 2, name: 'e'}, {id: 4, name: 't'}, {id: 2, name: 'e'}, {id: 0, name: 'q'}]},
{id: 2, name: 'c', tags: []},
{id: 3, name: 'd', tags: [{id: 7, name: 'i'}, {id: 3, name: 'r'}, {id: 0, name: 'q'}]},
{id: 6, name: 'g', tags: [{id: 7, name: 'i'}, {id: 4, name: 't'}]},
]
const tags = [
{id: 0, name: 'q'},
{id: 1, name: 'w'},
{id: 2, name: 'e'},
{id: 3, name: 'r'},
{id: 4, name: 't'},
{id: 7, name: 'i'},
{id: 11, name: 's'}
]
let selectedTags = [0, 5]
The challenge is to find tasks that include all of the selected tags based on their indexes in the tags
array.
Expected output:
let result = [
{id: 0, name: 'a', tags: [{id: 0, name: 'q'}, {id: 7, name: 'i'}]},
{id: 3, name: 'd', tags: [{id: 7, name: 'i'}, {id: 3, name: 'r'}, {id: 0, name: 'q'}]}
]
An attempted solution using lodash:
let result= []
_.forEach(selectedTags, index => {
const tagId = tags[index]._id
result = _.filter(tasks, task => _.find(task.tags, ['_id', tagId]))
})
However, this approach did not fulfill the criteria as it only retrieved objects with one selected tag. Any suggestions?