educative.io

Higher order functions explanation

I understand the basic idea of these two lines, but can someone explain the inner workings?
const byNolan = movie => movie.director === "Christopher Nolan";
const filter = (movies, func) => movies.filter(func);
const nolanMovieList = filter(movieList, byNolan);

Function byNolan returns an array of movie objects directed by Christopher Nolan. When filter is called, its second paramter is the ‘byNolan’ function. When it filters, what are the criterias it is using? the filter method returns an array of items that meet a certain criteria. So what kind of criteria is movies.filter(movieList, byNolan) using. I hope the question makes sense.

Hi @Siem,

Basically, the filter function is a higher-order function. In the first line of code, we get an array of the movie object directed by Christopher Nolan. After that, we define a helper function to achieve the desired result by using the filter() function by passing the movieList and byNolan array of a movie object. So the movies.filter(movieList,byNolan) using the criteria that we defined here filter = (movies, func) => movies.filter(func); in helper function. This will filter the movies from the movieList and provide you with the result.

I hope I have answered your query; please let me know if you still have any confusion.

Thank You :blush: