I have the following arrays:
const tasks = [
{id: 0, name: 'a', tags: [{id: 0, name: 'q'}, {id: 1, name: 'w'}]},
{id: 1, name: 'b', tags: [{id: 2, name: 'e'}, {id: 4, name: 't'}, {id: 11, name: 's'}]},
{id: 2, name: 'c', tags: []},
{id: 3, name: 'd', tags: [{id: 0, name: 'q'}, {id: 3, name: 'r'}, {id: 7, name: 'i'}]},
{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]
selectedTags is an Array of indexes of tags Array. Now I need to find all objects in tasks Array, where property tags includes any of the selected tags. So in this case the output should be:
let result = [
{id: 0, name: 'a', tags: [{id: 0, name: 'q'}, {id: 1, name: 'w'}]},
{id: 3, name: 'd', tags: [{id: 0, name: 'q'}, {id: 3, name: 'r'}, {id: 7, name: 'i'}]},
{id: 6, name: 'g', tags: [{id: 7, name: 'i'}, {id: 4, name: 't'}]}
]
I tried to do something like this:
let result= []
_.forEach(selectedTags, index => {
const tagId = tags[index].id
result = _.filter(tasks, task => _.some(task.tags, ['tag.id', tagId]))
})
but I'm ending up with an empty array. I tried to use map, find and some other lodash methods, but so far nothing worked.
Any ideas, please?
resulteach time through the_.forEachloop, not combining the results from previous iterations.tagproperty to matchtag.id