2

Can someone please tell me why we get the various results with the following? I.e., how can I parse these to understand what's going on in Javascript underneath the hood to see why they give the results they do:

[{name: 'Joe', age: 30},{name: 'Frank', age: 20},{name: 'Ryan', age: 50}].map(({name}) => name)
// ["Joe", "Frank", "Ryan"]
[{name: 'Joe', age: 30},{name: 'Frank', age: 20},{name: 'Ryan', age: 50}].map((name) => name)
// [{name: 'Joe', age: 30},{name: 'Frank', age: 20},{name: 'Ryan', age: 50}]

I thought I knew how objects work, but obviously I need to understand this topic in more detail, as I don't see why the two statements provide the results they do.

FZs
  • 16,581
  • 13
  • 41
  • 50
Crowdpleasr
  • 3,574
  • 4
  • 21
  • 37
  • 3
    In the first function, you are destructing the name from each object and in the second function, you have given an argument named `name`, hence, you are getting an object. – Hassan Imam Sep 09 '19 at 04:47
  • 2
    `.map` transforms one array into another depending on the callback return value. `({ prop })` in an argument list extracts the property named `prop` from the argument. `.map(name => name)` doesn't really do anything, it just creates a shallow copy of the array, so it'll look the same as the original array – CertainPerformance Sep 09 '19 at 04:48
  • Got it, with many thanks! – Crowdpleasr Sep 09 '19 at 04:50

0 Answers0