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.
indexOf you check if a substring belongs to a stringsome you check if any of the elements of the array fulfills the questionconst 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)