1

If I have the following array of objects:

    const movies = [
        {
            movieTitle: "The House That Jack Built",
            movieGenre: "Thriller",
            movieDirector: "Lars Von Trier",
            movieStarring: "Matt Dillon, Uma Therman",
        },
        {
            movieTitle: "Mother!",
            movieGenre: "Thriller",
            movieDirector: "Darren Aronofsky",
            movieStarring: "Jennifer Lawrence",
        },
        {
            movieTitle: "Hot Fuzz",
            movieGenre: "Comedy",
            movieDirector: "Edgar Wright",
            movieStarring: "Simon Pegg",
        }
    ]

and the following array:

const filters = ["Jennifer Lawrence", "Comedy"]

How can I create a new array containing only the objects that have a value that's in the filters array (in this case the last 2)?

Thanks!

0

1 Answer 1

2

Using filter, some and Object.values:

const movies  = [{movieTitle:"The House That Jack Built",movieGenre:"Thriller",movieDirector:"Lars Von Trier",movieStarring:"Matt Dillon, Uma Therman"},{movieTitle:"Mother!",movieGenre:"Thriller",movieDirector:"Darren Aronofsky",movieStarring:"Jennifer Lawrence"},{movieTitle:"Hot Fuzz",movieGenre:"Comedy",movieDirector:"Edgar Wright",movieStarring:"Simon Pegg"}];
const filters = ["Jennifer Lawrence", "Comedy"];

const res = movies.filter(
  movie => Object.values(movie).some(v => filters.includes(v))
);

console.log(res);

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

1 Comment

Perfect, thanks. Tried various combinations of these but couldn't get it right

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.