Home > Back-end >  How to Use One Array of Strings to Filter Another Array of Strings (JS)
How to Use One Array of Strings to Filter Another Array of Strings (JS)

Time:12-03

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"]

CodePudding user response:

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);

CodePudding user response:

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);

CodePudding user response:

Use the filter method to loop over the songs and return a new filtered array. For each song, check if every filter string is not present in the song.

const songs = ["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 filters = ["Remaster", "- Live in", "Remix"];

const filteredSongs = songs.filter(song => 
  filters.every(filter => !song.includes(filter))
);

console.log(filteredSongs);

  • Related