Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

JASON LEE
JASON LEE
13,654 Points

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.