Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use One Array of Strings to Filter Another Array of Strings (JS)

Given an array of songs that are different versions of the same song:

const songArray = ["Holiday", "Holiday - Remastered Remix", "Holiday - Live in Portugal", "Holiday (Remix)", "Like a Prayer", "Like a Prayer - Remaster", "Like a Prayer - Remixed 2012", "Music", "Music (Remix)" ]

How can I loop through using another array of "filter words" I've created:

const filterWords = ["Remaster", "- Live in", "Remix"]

To get back everything that does NOT include those filters.

ie:

const filteredSongs = ["Holiday", "Like a Prayer", "Music"]

I've tried finding the answer online, but I seem to only find examples that search for one filter word, not multiple.

I've tried nested looping, .includes(), .filter(), and I'm also having problems because some tracks will contain more than one of the filter words (eg. "Holiday - Remastered Remix" contains both "Remaster" and "Remix"), and so will be pushed to a new array twice.

something like:

    songArray = ["Holiday", "Holiday - Remastered Remix", "Holiday - Live in Portugal", "Holiday     (Remix)", "Like a Prayer", "Like a Prayer - Remaster", "Like a Prayer - Remixed 2012", "Music",    "Music (Remix)" ]


    const filterWords = ["Remaster", "- Live in", "Remix"]

    newArray = []

    songArray.forEach((song) => {
      filterWords.forEach((filterWord) => {
        if song.includes(filterWord){
           newArray.push(song)
    })
    })

expected: ["Holiday", "Like a Prayer", "Music"]

like image 200
DBN Avatar asked Oct 22 '25 07:10

DBN


2 Answers

You can achieve it using Array.prototype.filter(), Array.prototype.some(), and String.prototype.includes().

Array.prototype.some() makes your task easy and clear to read.

Try like this:

const songArray = [ "Holiday", "Holiday - Remastered Remix", "Holiday - Live in Portugal", "Holiday (Remix)", "Like a Prayer", "Like a Prayer - Remaster", "Like a Prayer - Remixed 2012", "Music", "Music (Remix)", ];

const filterWords = ["Remaster", "- Live in", "Remix"];

const output = songArray.filter(
  (song) => !filterWords.some((word) => song.includes(word))
);

console.log(output);
like image 100
Amila Senadheera Avatar answered Oct 24 '25 22:10

Amila Senadheera


You were pretty much there. Here is a way (based on what you had already done) that works. Not the best way though, check out the other answers for simpler methods.

songArray = [
  "Holiday",
  "Holiday - Remastered Remix",
  "Holiday - Live in Portugal",
  "Holiday     (Remix)",
  "Like a Prayer",
  "Like a Prayer - Remaster",
  "Like a Prayer - Remixed 2012",
  "Music",
  "Music (Remix)",
];

const filterWords = ["Remaster", "- Live in", "Remix"];

newArray = [];

songArray.forEach((song) => {
  let found = false;
  filterWords.forEach((filterWord) => {
    if (song.includes(filterWord)) {
      found = true;
    }
  });
  if (!found) {
    newArray.push(song);
  }
});

console.log(newArray);

like image 30
Emilien Avatar answered Oct 24 '25 21:10

Emilien