Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda