-1

I am failing to understand what the parentheses are doing in the map array method

type EmployedPerson = (Person & Employee);  

function correlateData(peopleData: Person[], staff: Employee[]): EmployedPerson[]
    {
        const defaults = { company: "MISSING", dept: "MISSING" };
        return peopleData.map(p =>
             (  { ...p, ...staff.find(empl => p.id === empl.id) || { id: p.id, ...defaults }}  )
        );
    }

It seems like I can't just return a literal object without wrapping it inside parentheses. From the theoretical point of view, what are the parentheses doing/what role do they have?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

2 Answers2

0

Surrounding parenthesis are there to differentiate between labda function syntax and lambda expression returning a value:

(p) => { /* TODO */ } // function body, needs `return` statement inside to return value.
(p) => p + 1; // returns value;
(p) => ({ firstName: "Joe"}); // returns new object initialized by literal notation
Nenad
  • 24,809
  • 11
  • 75
  • 93
  • Thank you Nenad, Even if I am writing angular since almost a year now, I didn't really returned a literal object through a fat arrow. It is kind of like logical, to have a separation between the body of a fat arrow and a literal object with some kind of notation. The choosing of round parenthesis was for me very confusing. But everything else was Cristal clear . – Iulian Radulescu Apr 18 '20 at 09:12
  • Ah, I updated the answer. No need for sarcasm. – Nenad Apr 18 '20 at 09:15
  • Please edit your answer only from the "Surrounding parenthesis.... " section and delete everything else. So that it's stays crisp for others. – Iulian Radulescu Apr 18 '20 at 09:23
  • @IulianRadulescu Answer is not only for you, but everyone who runs into the question. So I would prefer to have full expression explained. Is that OK? – Nenad Apr 18 '20 at 09:24
  • 1
    No because this is not what i asked. Or at least, move the important section on top. That should also do. – Iulian Radulescu Apr 18 '20 at 09:26
  • @IulianRadulescu Agree. It was to verbose. Thanks for feedback. – Nenad Apr 18 '20 at 09:29
-2

the () say that it is a single-line function ( (x)=>hello ${x}; ) and not a block-function ( (x)=>{let thing=hello ${x};return thing;} ) when you type p => {......} it sees it as a block-function and not as an object, typing p => ( {.....}||{.....} ) makes it clear that it's a single-line function

KhanKudo
  • 82
  • 1
  • 3
  • I can't upvote you for your "single-line function" (I am not privileged enough ) , because in this case it is applied. But the answer is only to separate the literal object syntax from the fat arrow body syntax. And that is it. – Iulian Radulescu Apr 18 '20 at 09:19