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

First, what do you think the parameter title represents above?

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?..

Let me ask it a different way. The first parameter of the callback function represents the element currently being iterated by the map method. What kind data type is that element in this particular case?

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

Fill in the blank:

watchList is an array of _________ with various properties.

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});

Alright, looks like you now understand what is required. Now you just need to return an object using a callback function. Currently, you do not have a callback function.

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});

I think I understand what you are attempting to do, but I want you to first explain it to me, so I know your intent.

Why are you putting the title, rating where you did an surrounding them with parentheses?

What kind of value should be returned in each iteration? What is the data type of the value that needs to be returned?

1 Like

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});

99% there…

The problem now is when you use => with { }, JavaScript treats the part between the { } as a code block instead of an object. So, you must either surround the { and } with parentheses or explicitly return the object. If you are not sure what I mean by “explicitly”, do a bit a research on arrow functions using an implicit return and an explicit return.

But this doesn’t work either. AAAAAAAAGHHH

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

That is because you are not properly creating an object literal.

An object is made of property/value pairs. You need to go back an review the proper syntax for property/value pairs in objects. You currently are not doing that correctly.

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}));

You should review how to add property/value pairs to objects. What does a literal object look like that has properties and values assigned to those properties?

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}));