Why my code is not working and I am getting error after so much hard work ?
var filteredList = watchList.map((item) => ({title: item.Title, rating: item.imdbRating})).filter((item) => (item.imdbRating >= 8.0));
console.log(filteredList);
Why my code is not working and I am getting error after so much hard work ?
var filteredList = watchList.map((item) => ({title: item.Title, rating: item.imdbRating})).filter((item) => (item.imdbRating >= 8.0));
console.log(filteredList);
I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>
) will also add backticks around text.
You’re right that you can chain map
and filter
but you should be aware of what comes out of each to put into the next
Think about what the result of the map
looks like, it’s an array of objects. Do these objects ever have the property imdbRating
?
For what it’s worth in this case I personally think it’s better to filter
before the map
, it should do less work
var filteredList = watchList.filter((item) => ({rating: item.imdbRating})).map((item) => ({title: item.Title, rating: item.imdbRating >= 8.0}))
I have rewrite my code this way but still its not working
Take a look at what you’ve got as the argument to filter
now, it’s not right
Edit: same with map
, be sure to remember what each argument to those should look like
Sorry guys, I completely lost and still can’t figure out
Thank you guys, I arrived at correct solution and below code is working. You are right @camperextraordinaire , I learnt a lot about filter and map now. many thanks
var filteredList = watchList.filter((item) => (item.imdbRating >= 8.0)).map((item) => ({title: item.Title, rating: item.imdbRating}))
I have one more question, why we don’t filter “title” in filter function and only use “rating” in filter ?
In your solution, you are first using the .filter
method to filter the list by imdbRating, which is what the asked. You could add a filter to look for “titles” that match a keyword or regular expression, but that is not what the problem was trying to get you to do.
Alternatively you could have first used the .map
method to extract the “title” and “imdbRating” from the “watchList” and then filtered it. But you will have to keep in mind that if you use the 2nd method, which arguments are you passing into the .filter
method; the original “watchList”, or the new listed created by .map
.
Thank you randelldawson. I was having a similar issue with setting up the the problem and your explanation really helped.
var filteredList = watchList.filter(item => item.imdbRating >= 8.0).map((item) => ({
title: item[“Title”],
rating: item[“imdbRating”]
}));
use this it worked for me
Many people posted “solutions” to this problem in multiple forums, and yours is the only one I got to pass. This is also very clean and minimal, nice going!