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 trialJenniffer Hernandez
Full Stack JavaScript Techdegree Student 11,662 PointsUsing the reduce method, extract all the customer hobbies into their own array. Store the hobbies in the hobbies array.
Cant seem to get my code to work any suggestions??
const customers = [
{
name: "Tyrone",
personal: {
age: 33,
hobbies: ["Bicycling", "Camping"]
}
},
{
name: "Elizabeth",
personal: {
age: 25,
hobbies: ["Guitar", "Reading", "Gardening"]
}
},
{
name: "Penny",
personal: {
age: 36,
hobbies: ["Comics", "Chess", "Legos"]
}
}
];
let hobbies;
// hobbies should be: ["Bicycling", "Camping", "Guitar", "Reading", "Gardening", "Comics", "Chess", "Legos"]
// Write your code below
hobbies = customers
.map(customer => customer.personal.hobbies.
.map(=>hobby => hobby))
.reduce((arr,hobbies)=>
[...arr,..hobbies],
[] );
3 Answers
Zimri Leijen
11,835 Pointshobbies = customers
.map(customer => customer.personal.hobbies.
.map(=>hobby => hobby))
.reduce((arr,hobbies)=>
[...arr,..hobbies],
[] );
there seems to be a typo here in your first map
method, you have a .
instead of a )
at the end.
you're also missing a .
in the spread operator on hobbies
there's also two arrows in your second map method.
Jenniffer Hernandez
Full Stack JavaScript Techdegree Student 11,662 PointsThank for your help !
Sean T. Unwin
28,690 PointsAs Zimri Leijen mentioned you have some syntax errors. As well as one other that I spotted which is you're missing the third dot (.
) at hobbies
in the reduce
method.
All together the errors are:
- In the first
map
there is a trailing dot (".
") where there should be a closing bracket (")
") - In the second
map
there is a leading arrow. This map isn't needed by the way as it's redundant - In the
reduce
there is a missing dot (.
) in the spread operator forhobbies
(always three (3) there is) - One other that I noticed in typing, is that in the
reduce
method the comma and Array declaration are not required.- i.e. Simply return
[...arr,...hobbies]
- i.e. Simply return
Great attempt and you'll get it with some refactoring!