Array.prototype.map() in Javascript

I’m in a problem known as, Functional Programming: Use the map Method to Extract Data from an Array.
Here is what I want,

[{"title":"Inception","rating":"8.8"},{"title":"Interstellar","rating":"8.6"},{"title":"The Dark Knight","rating":"9.0"},{"title":"Batman Begins","rating":"8.3"},{"title":"Avatar","rating":"7.9"}]

Here is what I did so far,

["{title : Inception,rating : 8.8}","{title : Interstellar,rating : 8.6}","{title : The Dark Knight,rating : 9.0}","{title : Batman Begins,rating : 8.3}","{title : Avatar,rating : 7.9}"]

My code,

var ratings = watchList.map(movie => "{"+"title : "+ movie.Title +","+ "rating : "+ movie.imdbRating +"}");

Why I can’t reach my expectation?

you are creating an array of strings, instead those needs to be objects

Use map on watchList to assign a new array of objects with only title and rating keys to the ratings variable.

1 Like

suggestion: make your code readable using appropriately named variables. Here using the name of user does not make sense, it is not related to what each array element rapresents in any way

1 Like

Thank you. I change user to movie.

If I use just movie then the format is okay. But I don’t know how to choose just the title and rating.
Capture
This way is also not working. (miss for loop :confused:)

var names = watchList.map(movie => movie.Title);
var rates = watchList.map(movie => movie.imdbRating);
var ratings = [
    {"title" : watchList.map(movie => movie.Title), "rating" : watchList.map(movie => movie.imdbRating)}
];

Give me some tips :frowning:

why did you choose this way of writing the string?

you need to create an object, this is taught in the Build JavaScript Objects lesson

1 Like

The basic form of a map is

let myNewArray = myOldArray.map(
    (oldEntry) => {
        /* some code */
        return newEntry
    }
);

so the function inside of the map should be building the contents of the new array out of the contents of the old array, entry by entry.

The question becomes, if you have a single entry in the watchList array, how can use use it to create a single entry for the ratings array?

1 Like

It was wrong to choose this way :roll_eyes:

I try this but it’s giving me an array with empty objects.

var ratings = [watchList.map(movie => {
  let obj = {};
  obj["title"] = obj[movie.Title],
  obj["rating"] = obj[movie.imdbRating]
  return obj;
})];

You’re close, but I’m not sure I understand this syntax here? This says 'look for the value of property of obj that has the key corresponding to the contents of movie.Title'.

Do you need the obj[] part? What is your intent with that part?

1 Like

also, remember that map() returns an array, which you are putting in an array - you do not want a bidimensional array there, do you?

2 Likes

Yeahhh :partying_face: :partying_face: :partying_face:

var ratings = watchList.map(movie => {
  let obj = {};
  obj["title"] = movie.Title,
  obj["rating"] = movie.imdbRating
  return obj;
});

This isn’t how you assign properties to new objects. You are trying to assign the poperty to itself, which is impossible. I’d echo @ieahleen and say you need to review how objects work, you’re fundamentally misunderstanding some things here.

The object you are accessing is movie in the function, the parameter of the callback is the object.

Edit: yes, that’s how you do it. Do you understand fully what you’ve written here, why the previous attempts were incorrect?

1 Like

if you undersand how objects work you can also do it using the implicit return, can you?

1 Like

Does the implicit return work here? I kept getting a strange syntax error of some sort when I tried.

Edit: ahhhhh, @ArielLeslie showed me the magic

to return an object with implicit return, put it inside round parenthesis ()

2 Likes

Ya, I understand. movie.Title is movie name. obj[movie.Title] doesn’t make any sense. As @JeremyLT said, This says 'look for the value of property of obj that has the key corresponding to the contents of movie.Title '.

1 Like

It’s always a fun day when I can learn something new!

1 Like

Okay :sob:
Is that what you meant?

var ratings = watchList.map((movie) => ({
  "title" : movie.Title,
  "rating" : movie.imdbRating
}));

It was so new and cool :gift_heart:
Helpful Article: Arrow function expressions - JavaScript | MDN

1 Like