I have an array "link" of objects, the objects have a feild that is another array. Then I have an array "valuesAces" of strings
I would like to filter "links" in order to get the objects that have at least one of the values in "valuesAces".
In the following example , "linksFiltered" needs to end up being :
[
{aces:['A','B']},{aces:['A','C','D']},{aces:['B']},{aces:['D','B']}
]
Example :
let links = [
{aces:['A','B']},{aces:['A','C','D']},{aces:['B']},{aces:['D','C']},{aces:['D','B']}
]
let valuesAces = ['A','B']
linksFiltered = links.filter(link => { return
[...link.aces].some(ace => valuesAces.includes(ace)
)})
I am apparently doing something wrong as the result is not the one expected. Any advicewill be very appreciated
const links = [
{aces:['A','B']},
{aces:['A','C','D']},
{aces:['B']},
{aces:['D','C']},
{aces:['D','B']}
]
const valuesAces = ['A','B']
const linksFiltered = links.filter(link => link.aces.some(ace => valuesAces.includes(ace)))
First off, you're missing commas in your links
let links = [
{aces:['A','B']},
{aces:['A','C','D']},
{aces:['B']},
{aces:['D','C']},
{aces:['D','B']}
]
Then you had one extra ) after .includes(ace)))). It should be:
let valuesAces = ['A','B']
linksFiltered = links.filter(link => { return (
([...link.aces].some(ace => valuesAces.includes(ace)))
)})
But why are you putting so many parentheses? This is the same code:
linksFiltered = links.filter(l => l.aces.some(a => valuesAces.includes(a)))
Finally, for better readability, you can change it to:
const inValuesAces = x => valuesAces.includes(x)
linksFiltered = links.filter(l => l.aces.some(inValuesAces))