Mei
October 16, 2019, 5:38pm
1
Hi Everyone.
So I’m currently going through the Javascript course, and there is a particular section in the “Using .map() to extract data from an array” that I don’t understand.
In ES6, I wrote the code as:
var rating = watchList.map(e => ({
title: e.Title,
rating: e.imdbRating
}));
Which works perfectly fine, however if I were to change it to ES5.
var rating = watchList.map(function(e){
title: e.Title,
rating: e.imdbRating
});
It spits out a syntax error. Can someone explain the reason for this?
var rating = watchList.map(function(e){
title: e.Title,
rating: e.imdbRating
});
This function doesn’t return anything and is also missing the braces ({}
) around the object.
Mei
October 16, 2019, 8:25pm
3
Got it!
var rating = watchList.map(function(e){
return ({title: e.Title,
rating: e.imdbRating})
});
Thanks for the help!
1 Like
Good job figuring it out. Happy coding!
Onosgb
October 16, 2019, 9:08pm
5
you are suppose to use the return key word . example.
return {
title: e.Title,
rating: e.imdbRating
}
});
ILM
October 17, 2019, 12:34pm
6
not necessarily, with arrow syntax if your function body is just a return statement you can use implicit return
the following functions do the same thing
x => {
return x/2
}
// the above is the same as the below
x => x/2