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 trialLisa Carbonell
Full Stack JavaScript Techdegree Graduate 16,926 PointsConfused...
hobbies = customers .map(customer => customer.personal.hobbies.map(hobby => hobby)) .reduce((arr, cur) => [...arr, ...cur.personal.hobbies], []);
console.log(hobbies);
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, cur) => [...arr, ...cur.personal.hobbies], []);
console.log(hobbies);
2 Answers
Steven Parker
231,248 PointsYou're working too hard — you don't need "map" for this challenge. In fact, you've done such a great job with the "reduce" function that if you just remove the "map" line completely you will pass the challenge.
Happy coding!
Ross King
20,704 PointsHi Lisa Carbonell ,
Steven Parker is correct and it will give you the best result by removing the map function. I wanted to let you know why you're code is resulting in an error (or challenge fail).
.map is returning an array of hobbies e.g. ["Bicycling", "Camping", "Guitar", ...rest] .reduce is trying to access an array item with the properties of personal.hobbies, however this is undefined as you have just converted the array into strings instead of objects.
For your code to with a map function, the following example will work.
hobbies = customers
.map(customer => customer.personal.hobbies)
.reduce((arr, cur) => [...arr, ...cur], []);
As Steven answered previously, the following is the best way to solve this challenge:
hobbies = customers
.reduce((arr, cur) => [...arr, ...cur.personal.hobbies], []);