Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
filtering an array of objects that has nested arrays, with an array of values

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

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0


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)))

about 4 years ago · Juan Pablo Isaza Relatório

0

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))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda