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

Peter Retvari
seal-mask
.a{fill-rule:evenodd;}techdegree
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 Points

Maybe it's not a proper way, but it's shorter and you got the same result

HI guys,

Maybe it's not a proper way, but it's shorter and you got the same result:

const users = [
  {name: 'Samir', age: 32},
  {name: 'Angela', age: 25},
  {name: 'Beatrice', age:56}
];

const reducedUsers = {};

users.reduce((acc, curr)=> reducedUsers[curr.name] = curr.age ,0);

console.log(reducedUsers);

I created an object, which will hold the value (acc). Each iteration I give this object a new key value pair with the current name and age value.

2 Answers

Jordan Kittle
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jordan Kittle
Full Stack JavaScript Techdegree Graduate 20,138 Points

This is the first workshop where I wasn't able to come up with the solution to the challenge on my own. I think expecting us to figure out we need to pass an empty object as the initializer and return the object each iteration is a bit too much considering we never learned about that in the lesson.

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 10,725 Points

You are correct that this is a way to do this with less code. However, I recommend that you follow along with the concepts covered in the video. It is better that you learn all the different ways to do something instead of just one way. Especially since the .reduce() method you are using will be covered in a later video.