0

Why does this work:

const final = pdata.map((p) => p.nodeName);

// returns [ 'H1', 'P', 'P' ] like its supposed to

But this returns undefined in all of them:

const final = pdata.map((p) => { 
  p.nodeName
});

// returns [ undefined, undefined, undefined ]

I need to add a couple if statements inside to check for the different types but the {} seems to break it. Am I not supposed to this this in a .map()? Or is there another way to do this?

1 Answers1

6

The usage of {...} is to encapsulate multiple statements.

You need to specify the return keyword:

const final = pdata.map((p) => { 
  return p.nodeName;
});
Mamun
  • 66,969
  • 9
  • 47
  • 59