Use the filter method to extract data from an array
Hints
Hint 1
This challenge is solved in 2 steps.
Array.prototype.filter()
is used to filter the array so it’s left with elements that have imdbRating > 8.0.
Array.prototype.map()
can be used to shape the output to the desired format.
Solutions
Solution 1 (Click to Show/Hide)
const filteredList = watchList
.filter(movie => {
// return true it will keep the item
// return false it will reject the item
return parseFloat(movie.imdbRating) >= 8.0;
})
.map(movie => {
return {
title: movie.Title,
rating: movie.imdbRating
};
});
Code Explanation
First we filter
through and only return the objects that meet the criteria. In this case the criteria is having an imdbRating of 8.0 or higher.
Then we map
the objects to the desired format.
Solution 2 (Click to Show/Hide)
const filteredList = watchList
.filter(movie => movie.imdbRating >= 8.0)
.map(movie => ({ title: movie["Title"], rating: movie["imdbRating"] }));
// Only change code above this line
console.log(filteredList);
Solution 3 (Click to Show/Hide)
// Only change code below this line
const filteredList = watchList
.filter(({ imdbRating }) => imdbRating >= 8.0)
.map(({ Title: title, imdbRating: rating }) => ({ title, rating }));
// Only change code above this line
console.log(filteredList);