0

How can you filter movie title on the basis of Genre item in array?

const result = [
  {
    title: "Mad Max: Fury Road",
    genre: ["Action", "Adventure", "Sci-Fi"],
  },
  {
    title: "The Hunger Games: Mockingjay Part 1",
    genre: ["Adventure", "Thriller"],
  },
  {
    title: "Jurassic World",
    genre: ["Action", "Adventure", "Sci-Fi"],
  },
  {
    title: "Everest",
    genre: ["Drama", "Thriller"],
  },
  {
    title: "Insurgent",
    genre: ["Adventure"],
  },
  {
    title: "Sicario",
    genre: ["Action", "Crime", "Drama"],
  },
];

Suppose if I want to filter movie title name on the basis of genre eg: "Sci-Fi" then it should return array of movie titles eg: ["Mad Max: Fury Road", "Jurassic World"].

Tried various combinations of map and filter but not working.

const newResult = result
  .map((curr) => curr.genre.filter((ele) => ele === search))
  .filter((ele) => ele);

3 Answers 3

2

We use filter to return all movies that pass some condition.

For the passing condition of filter, we use every on the supplied array of genres.

every returns true if all of the elements in the array (genreList) it was called on return true for some condition.

For the condition of every, we check that the movie's genre array includes the entry in the given genreList array.

So in english, this code says "Please give me all movies that have all of the genres given in genreList".

const result= [
    {
      "title": "Mad Max: Fury Road",
      "genre": [
        "Action",
        "Adventure",
        "Sci-Fi"
      ]
    },
    {
      "title": "The Hunger Games: Mockingjay Part 1",
      "genre": [
        "Adventure",
        "Thriller"
      ]
    },
    {
      "title": "Jurassic World",
      "genre": [
        "Action",
        "Adventure",
        "Sci-Fi"
      ]
    },
    {
      "title": "Everest",
      "genre": [
        "Drama",
        "Thriller"
      ]
    },
    {
      "title": "Insurgent",
      "genre": [
        "Adventure"
      ]
    },
    {
      "title": "Sicario",
      "genre": [
        "Action",
        "Crime",
        "Drama"
      ]
    }
  ];
  
const moviesByGenres = (moviesList, genreList) => {
  return moviesList.filter((m) => {
    return genreList.every((g) => m.genre.includes(g));
  });
}

// All action movies
console.log(moviesByGenres(result, ["Action"]));

// All action+adventure movies
console.log(moviesByGenres(result, ["Action", "Adventure"]));

Sign up to request clarification or add additional context in comments.

Comments

1

Use Array#filter to select the relevant items that meet the set criteria and use Array#map to select the properties of the selected items that you're interested in.

const result= [ { "title": "Mad Max: Fury Road", "genre": [ "Action", "Adventure", "Sci-Fi" ] }, { "title": "The Hunger Games: Mockingjay Part 1", "genre": [ "Adventure", "Thriller" ] }, { "title": "Jurassic World", "genre": [ "Action", "Adventure", "Sci-Fi" ] }, { "title": "Everest", "genre": [ "Drama", "Thriller" ] }, { "title": "Insurgent", "genre": [ "Adventure" ] }, { "title": "Sicario", "genre": [ "Action", "Crime", "Drama" ] } ],
  
      search = "Sci-Fi";

      newResult = result
      //which items meet the set criteria?
      .filter(({genre}) => genre.includes(search))
      //what about those items am I zeroing in on?
      .map(({title}) => title);

console.log( newResult );

Comments

0

Found the answer after trial and error

const n = result.filter((curr) => curr['genre'].includes("Action"))

2 Comments

Do check my solution as well. It supports filtering by multiple genres at the same time. So if you want to filter movies that are, e.g. both "Action" and "Adventure" at the same time, you can do that.
Thanks this was useful, however requirement is that suppose if i want to filter in search box for movie it should take only 1 genre type not an array of search but thanks a lot for taking time and helping :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.