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

Steven Vallarsa
Steven Vallarsa
10,842 Points

Why did this solution work to the first filter/map problem in this video?

I tried many ways to solve the first filter/map coding challenge in the video, and accidentally stumbled upon a way that generates the correct result, but is different from the way Joel showed as the solution:

const userNames = ['Samir', 'Angela', 'Beatrice', 'Shaniqua', 'Marvin', 'Sean'];
    // Result: [{name: 'Samir'}, {name: 'Shaniqua'}, {name:'Sean'}];

const users = userNames
    .filter(name => name[0] === "S")
    .map(name => {
      return {name}  
    });

console.log(users);

Why is it that just using {name} allows that variable to be the key in the array of objects?

1 Answer

Steven Parker
Steven Parker
216,057 Points

When you write an object literal, giving only a variable name creates an attribute with the same name as the variable, and gives it the value of the variable.

so if name = "Dave", then { name } is the same as { name: "Dave" }.