Functional Programming - Use the map Method to Extract Data from an Array

Can anybody explain how to achieve this without arrow function notation?
I’m trying to replicate it using function so that I can really understand what is going on, but I can’t replicate the output.

This is the arrow function

({ Title: title, imdbRating: rating }) => ({title, rating})

Have you tried rewriting this as a non-arrow function yet? That’s a great place to start. If you get stuck, post what you have so far!

Create a named function that does what you want, inside the main function.

Then add the function name as the parameter for the map method.

It’s a good idea not to post solutions on the forum.
Please edit your post to remove your solution.

I have thanks. This is my attempt:
const ratings = watchList.map((function({Title: title, imdbRating: rating}){return title,rating}))

But it only returns the rating.

Let’s look at only the function

function ({Title: title, imdbRating: rating}) {
  return title, rating:
}

This says the return value is title, rating. The comma operator is confusing and not doing what you think here. What is supposed to be in the array? Objects, right?

The output should be an array like this:

[{“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”}]

Each property is listed with it’s name and value.
I can achieve this with arrow notation, but struggling with function.

So what kind of thing is

That’s an object, right? How do you create an object?

A function can only return a single value. If you need to return more than one value they must be inside a data structure that can contain multiple values (such as an array or object.) The arrow function returns an object. Your function is trying to return a list of values, which it can not.


Are you sure you didn’t just copy the answer and believe that counts as you doing it?

Please, write a map for me that returns just the age properties of the objects. Rename the property to userAge as well. Use whatever syntax you like.

const users = [
  { name: "John", age: 34 },
  { name: "Amy", age: 20 },
  { name: "camperCat", age: 10 },
];

const justAges = users.map();
console.log(justAges); // [ { userAge: 34 }, { userAge: 20 }, { userAge: 10 } ]

Thank you all. I’ve worked out what I was doing wrong. I’ve solved with Object Literals.
Appreciate the clues.

Thank you for taking the time to reply.

I believe the answer is:

const users = [
{ name: “John”, age: 34 },
{ name: “Amy”, age: 20 },
{ name: “camperCat”, age: 10 },
];
const justAges = users.map((user) => { return { userAge: user.age }}
);
console.log(justAges)

Yes.

What if I wrote it like this?

const justAges = users.map(({ age }) => ({ userAge: age }));

Can you try re-writing that to use a normal function?

I would do this:

const justAges = users.map(function(user){ return {userAge: user.age}})

But I noticed you are only passing age into your function, where as I am passing the whole user object.

I tried to deconstruct it in the function input so as to just pass in user.age, but couldn’t quite figure it out.

I assume it is faster to only pass in the properties you need?

That is correct.

It uses destructuring to get the property instead of using the entire object. It would look like this with your function.

const justAges = users.map(function ({ age }) {
  return { userAge: age };
});

There isn’t a single good reason to use one or the other. Sometimes, you might want the object, and sometimes just the properties. You also do error handling differently depending on which you use (e.g. if the function is passed null or undefined).

It is also just a code-style thing.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.