Unable to pass "Use the filter Method to Extract Data from an Array"

I can’t seem to pass this test, and I’m pretty sure it’s because the ratings numbers need to be converted back into strings. Does anybody know how to do that? The toString() method doesn’t seem to be working.

Here’s my code:

const filteredList = watchList.map(film => {
  return {
    title: film.Title,
    rating: parseFloat(film.imdbRating)
  }
}).filter(film => film.rating >= 8.0);


console.log(JSON.stringify(filteredList));

Try to use . filter on filteredlist

1 Like

Try filtering first and then doing the mapping. Don’t know if the order matters, but it worked for me that way.
Also, do you need the return within map? Could that be affecting the code somehow? I honestly don’t know, but when mappin I just did it like this:

.map(movie => ({
   title: movie["Title"],
   rating: movie["imdbRating"]
 })

I suggest doing just either the map or the filter first and console.log that to see if things are going as you expected.

I hope that helps!

2 Likes

Thanks! Yes, I think you’re right. From the research I’ve done it looks like filtering first, then returning the filtered array as a new array with map is probably going to work.

I’ll give it a try and let you know if it worked.

1 Like

Okay, check this out. I did a Google search and found something called the unary plus operator, which forces JavaScript to treat a string like a number. The cool thing about it is it doesn’t change the string to a number, but instead ignores the string state in order to perform mathematical operations. Here’s my new, updated, working beautifully, code:

const filteredList = watchList.map(film => {
  return {
    title: film.Title,
    rating: film.imdbRating
  }
}).filter(film => +film.rating >= 8.0);


console.log(filteredList);

Sweet!

1 Like