Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Asynchronous Programming with JavaScript Exploring Async/Await Convert Promise Handling to Async/Await

About the object returned from the .map() callback

Hi,

Could someone explain the following syntex to me?:

return { ...profileJSON, craft}

If I understand the spread operator syntax, profileJSON is json data which by the spread operator is converted to an array of indivdual profiles? Does the comma syntax followed by craft mean that the craft constant is appended to each iteration of the .map() call back?

Guess I juest answered my own question; The .map() callback iterates through each peopleJSON object, fetches a single profile from wiki api with each iteration, returning a single object with each iteration, containing both a single profile and a single craft value from open notify API.

I'll just leave this here for others, please correct me if I'm wrong.

1 Answer

The ... spread operator syntax copies the object. Without the spread operator it will append the object by reference to the variable or return value.

I tried playing around in the console...

  • A = ...profileJSON - does not work. syntax error.
  • B = {...profileJSON} - it works. The variable B is stored the copied contents of the profileJSON object. The two are independent.
  • C = profileJSON - similar to B it seems, except it appends the property value pairs of the profileJSON by REFERENCE, pointing to the same block in memory or something like that. Any change in profileJSON also changes C and vice-versa .
  • D = {profileJSON} - this is confusing. It seems to append the object into the object I think. So like D is an object which contains the object profileJSON.