Tell us what’s happening:
I wanted to test out parameter deconstructing and wrote this code and it matches character to character for the solution. But it’s giving me an unexpected return value.
Your code so far
There is an array called watchList, which is an array of objects containing information about a number of movies.
const ratings = watchList.map(({ Title: title, imdbRating: rating}) => ({title: rating}));
// console output
[{"title":"8.8"},{"title":"8.6"},{"title":"9.0"},{"title":"8.3"},{"title":"7.9"}]
[{"title":"8.8"},{"title":"8.6"},{"title":"9.0"},{"title":"8.3"},{"title":"7.9"}]
Challenge: Use the map Method to Extract Data from an Array
Link to the challenge:
Correct solution has title and rating fields. You have only title. With a value of rating.
Solution 2
const ratings = watchList.map(({ Title: title, imdbRating: rating }) => ({title, rating}));
My Code
const ratings = watchList.map(({ Title: title, imdbRating: rating}) => ({title: rating}));
They are exactly the same… I’m assuming your reply is meant for Solution 1.
You are absolutely right. I think my brain needs some rest. Thank you
I know this is not the correct solution but why is this code syntactically incorrect?
var ratings = watchList.map(movie => ({
movie.Title: movie.imdbRating}));
I kept getting an error of
expected ,
ilenia
February 18, 2021, 4:33pm
7
the dot is not a valid character in a variable, so if you want to use movie.Title
as property name you need to wrap it in quotes "movie.Title": movie.imdbRating
1 Like
Or if you want the value of movie.Title
to be the key:
var ratings = watchList.map(movie => ({
[movie.Title]: movie.imdbRating})
1 Like
Thank you just the explanation I needed!
Ahh this is also the explanation I needed thank you!
1 Like
system
Closed
August 21, 2021, 2:47am
11
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.