0

I'm trying to clean this func (Y) and discovered something I don't understand. The idea is to return a certain number of {objects} from the func.

let y = watchList
  .map(function (item) {
    return { title: item["Title"] }
});

//[{…}, {…}, {…}, {…}, {…}]

let x = watchList
  .map(item => {
    title: item["Title"]
    }
  );

//[undefined, undefined, undefined, undefined, undefined]

Y lets me create an Object inside the func, but with the arrow functions on X I'm not able to do so.

Why is this happening?

Thank you.

Tiago Ruivo
  • 183
  • 1
  • 9

1 Answers1

1

You need to wrap the object in parenthesis for this to work, otherwise it get interpreted as the function's body, and you end up with a function without return.

let x = watchList
  .map(item => ({
    title: item["Title"]
  })
);
FZs
  • 16,581
  • 13
  • 41
  • 50
Clarity
  • 10,730
  • 2
  • 25
  • 35
  • I got it: " because without parens js parser thinks that its a function body, not an object, and foo is a label" that's why! – Tiago Ruivo Dec 12 '19 at 13:25