Map with 2 parameters

Tell us what’s happening:
I’m trying to solve the Use the map Method to Extract Data from an Array challenge. I can get this line to work:

ratings = watchList.map(title => title.Title);

but I am totally unclear on how to add the rating in as part of the same array. This doesn’t work:

ratings = watchList.map(title => title.Title,rating => rating.imdbRating);

Should I be using 2 separate map statements?

**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36</code>.

**Challenge:** Use the map Method to Extract Data from an Array

**Link to the challenge:**
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/use-the-map-method-to-extract-data-from-an-array

map doesn’t accept two functions as parameters, only one

so the callback function in this way is returning a string title => title.Title and the map method is creating a new array where in place of the objects that were in the original array now there are strings

what kind of data type does it needs to be if not a string? an array, an object?
make your callback function return that data type

A new array element in the array “ratings”?

The callback needs to return an object to populate ratings?..

In this case it’s a string (as are the rest)…?

watchList is an array of objects with various properties. title is a property of that object containing a string.
Ok so I need to return an object; the syntax eludes me. This doesn’t work either:

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

So I want something like this? This syntax isn’t working.

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

which fails on the arrow function before it ever gets to title.Title…part. Nor does this:

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

Each iteration should be a single object where the parameters title and rating are matched. So this isn’t working either.

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

Am I getting closer? lol

ratings = watchList.map(title=>{title.Title,title.imdbRating});

But this doesn’t work either. AAAAAAAAGHHH

ratings = watchList.map(title=>({title.Title,title.imdbRating}));

I thought I was 99% there! This doesn’t work either. I’m really frustrated with this now.

ratings = watchList.map(title=>({watchList.title.Title,watchList.title.imdbRating}));

I’m guessing this is closer but it’s returning everything instead of the correct items.

let ratings = watchList.map(title=>({Title:title, imdbRating:title}));

Do I need to be including the object name? I understand

object {
 property: value;
}

and

object['property'] = value;

but I don’t see how that helps referencing it inside of map.

just the graph parenthesis and the stuff inside it

you need to things

  1. figure out what property names you need to use
  2. figure out how to reference the value that you want to give to the property for each starting object

title is the object (maybe not the best parameter name… I think it is confusing you - why don’t you use obj or something as parameter name?)
do you want that as property value? or you want something from inside it as property value?

1 Like

Ok, is this closer? I haven’t figured out getting both Title and imdbRating yet.

ratings = watchList.map(obj=>(obj['Title']));

Edit: looking at this I see I am right back where I started. Ok I give up for today.

you are not returning an object, but you are getting one of the values you wanted, aren’t you?
you just need to put that one inside the right data structure

Just with respect to the difficulties you are having with syntax: you don’t need to complicate things further by trying to figure out the subtleties of arrow functions as well, you can just write it in the way you’ve been shown many times earlier in the course

.map(function (argument) {
  return // thing you are doing with argument
});

So in reading the challenge I totally missed that “title” and “rating” were to be the new object keys in a returned array and instead I was trying to return just an array containing the Title and imdbRating properties only (basically a double array). DUH

Basically I was trying to do something like this:

ratings = watchList.map(obj=>((obj['Title'],obj['imdbRating'])));

(which STILL doesn’t work, and only returns the rating…why?) and when I compared with what the challenge said I should be getting I realized I was leaving out the part with “title” and “rating” keys altogether. Now I totally “get” what @camperextraordinaire was hinting at the entire time. I ended up with this.

ratings = watchList.map(obj=>({"title":obj['Title'],"rating":obj['imdbRating']}));

Thank you @ilenia for cluing me in with my confusion regarding “title”… Sorry for my poor reading comprehension folks! Wow that was my entire Sunday!
Cheers

1 Like

Ok thanks. I am going to try it without using the arrow function now!

1 Like