Use the map Method to Extract Data from an Array

Use the map Method to Extract Data from an Array
0.0 0

#1

Hello there - I can’t figure out how to use the map method to pass this test. Specifically, I can’t seem to pair the strings “Title” or “rating” with the title of each movie and the IMDB rating. The best I got is the following, where I am able to extract the titles and ratings per se.

let rating = watchList.map(x => [x.Title, x.imdbRating]);
// console.log(rating) results in the following: [["Inception", "8.8"], ["Interstellar", "8.6"], ["The Dark Knight", "9.0"], ["Batman Begins", "8.3"], ["Avatar", "7.9"]]

What am I doing wrong here?


#2

You’re returning an array, so you get an array of arrays back. Return an object. {title: foo, rating: bar}.

Edit: note, if you want it on one line, same as you have, you need to wrap the object in brackets like ({title: foo, rating: bar}) to stop JS getting confused


#3

Thank you so much! This helped me finish the exercise. I can’t even tell you how long I was stuck!


#4

Also it’s convention and makes dubughing easier for you and others to name your iterated element as a singular of the array or an abbreviation of it, so instead of x, you may want to juts call it movie, mov or m


#5

Noted, that makes a lot of sense. I’ll do that from now on!