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 JavaScript Array Iteration Methods Combining Array Methods Working with Objects in Arrays

Frank keane
Frank keane
8,074 Points

Why is return necessary here?

Wondering why this code requires return. const usersObj = users.reduce((usersObj, user) => { usersObj[user.name] = user.age; return usersObj; }, {}); console.log(usersObj);

It says in the video that it works ion the first iteration but without the return cant read the second one. Why would it work on the first round?

Muchos!

1 Answer

Ryan Agatep
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Ryan Agatep
Full Stack JavaScript Techdegree Graduate 17,705 Points

Hmm. I don’t know exactly why, but my guess is this has something to do with a function that should only have one output. The object instance ‘usersObject’ was only available once during the first iteration. Since the function didn’t return the object instance, it didn’t have anywhere to put ‘Angela’ during the second iteration.