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

126
Visualizações
Javascript: Is there a way to check if an array contains at least one element from another array?

Is there a way to test whether or not an array contains at least one element from another array?

let array1 = [`a`, `b`, `c`, `d`, `e`];
let array2 = [`c`];

Basically is there a way to determine if array2 has any of the elements from array1 in a single step. I realize I could check to see if array2 has a or b or c or d or e, but I'm curious if there is a method that helps me.

includes() only allows me to check for one value. some() would allow me to test if c is included in array1 but not test if any of the five elements from array1 are part of the array2.

Is there some way of using reduce() or filter()?

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

0

You could take Array#some with a Set.

const
    array1 = ['a', 'b', 'c', 'd', 'e'],
    array2 = ['c'],
    hasValue = array2.some(Set.prototype.has, new Set(array1));

console.log(hasValue);

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