I wrapped your answer in [spoiler]
and [/spoiler]
tags - that keeps people from seeing the answer unless they want to.
I started off with filter()
but as I understand it map()
and filter()
can be used in either order. Is this a correct statement?
Yes. I think it is slightly more efficient to filter first so you don’t have to map values you will throw away any way, but you will get the correct answer either way. It won’t matter much in this case, but in large data sets, it might. It’s good to start thinking that way.
Your filtering is basically right. I would just make few suggestions.
parseInt(myFilm["imdbRating"])
First of all, it should be parseFloat. Since you are checking it against 8.0, which is essentially an integer, it works because parseInt will just round down to the nearest integer. So, it works here, but wouldn’t work if it were an actual floating number you were comparing it to.
Secondly, it would be more common to write myFilm["imdbRating"]
as myFilm.imdbRating
. The bracket notation is used if the property is not a proper JavaScript identifier, like if it has a space or is a variable.
But this works and will give the correct result.
The map function is flawless.
I linked this filtering to my mapping of the title and rating from watchList
array. Since I did filter()
first, the map()
function inherits the process of filtering ratings that are greater than or equal to 8
Yes, the results of the filter get fed into the map.
Is “inherit” the correct word to use here?
The technical term is “chaining” or “cascading” methods. You could have broken this into two steps, but as long as it’s clear, chaining is cool.
For clarity, I might have formatted it as:
const filteredList = watchList.filter(f => (parseFloat(f.imdbRating) >= 8.0))
.map(f => ({ title: f.Title, rating: f.imdbRating }));
or something like that. If the callbacks were more complicated, I might have put them in their own function, but these are probably simple enough. It’s common in a simple callback to use a one letter variable name. I chose “f” for “film”. But it isn’t required.
I used dot notation because that’s what I used in the previous map()
challenge. Yet, I tried out bracket notation and I also passed …
Yes, bracket notation will always work. But generally is if the property is not a variable and is a valid JS identifier, the dot notation is preferred as it is cleaner and easier to read.
But, great work. Keep it up. This is confusing stuff and it’s great when it finally starts to click. Remember that feeling - there will be more things like this, but if you keep at it, you’ll get them.