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

106
Visualizações
How to only keep array elements that are present as substring in another array

So the first array looks like this:

const allNewspaper = [
        'wall_street_journal_1631199606466.csv'
        ,'newyork_times_1631199606505.csv'
        ,'washington_post_1631199606516.csv'
];

Then I have another array containing all the newspaper that I want to keep in the previous array. But they are written without timestamp. So the second array looks like this:

const wantedNewspaper = [
        'wall_street_journal'
        ,'washington_post'
];    

At the end, a new array should look like this:

const allAcceptedNewspaper = [
        'wall_street_journal_1631199606466.csv'
        ,'washington_post_1631199606516.csv'
];  

I wonder if there is a simple way to do this with array.prototype.filter. My current approach is to loop through "allNewspaper" and check if there is an element in "wantedNewspaper" where a substring matches.

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

0

  • With indexOf you check if a substring belongs to a string
  • With some you check if any of the elements of the array fulfills the question
  • Combined you have your wanted filter :)

const allNewspaper = [
        'wall_street_journal_1631199606466.csv'
        ,'newyork_times_1631199606505.csv'
        ,'washington_post_1631199606516.csv'
];


const wantedNewspaper = [
        'wall_street_journal'
        ,'washington_post'
];    



const allAcceptedNewspaper = allNewspaper.filter(paper => wantedNewspaper.some( w => paper.indexOf(w) > -1 ) );

console.log(allAcceptedNewspaper)

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