I need to find all objects in multiple arrays with a specific value, returning another value of this image of all matching objects.
I'll try to make it a bit clearer with an example. I'm searching for every target with the value find-me and get the source value returned.
Some arrays have matching objects, some may not have. The result array should have unique values.
const deps = {
"something": [
{
"type": "static",
"source": "foo",
"target": "bar"
},
{
"type": "static",
"source": "return-me",
"target": "find-me"
}
],
"anything": [
{
"type": "static",
"source": "and-me",
"target": "find-me"
}
],
"no-match": [
{
"type": "static",
"source": "foo",
"target": "bar"
}
]
}
So for this example, the result should be
['return-me', 'and-me']
I tried this:
const search = 'find-me'
const sources = Object
.values(deps)
.flat()
.find(el => el.target === search)
.map(el => el.source)
But of course this can't work, as find will give me only one result (which is an object). How do I get all results instead of first matching object?