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

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
229,004 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" }.