Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!