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 trialLainey Odette
18,618 PointsI really don't know what else to try in this reduce() challenge...
Hello, I've clearly not understood the concepts in this lesson, but I've reviewed my notes and MDN and read more in other resources and I'm just completely stuck. I have been working on this challenge for over an hour trying every possibility - even ones I absolutely knew would not work.
The error I'm receiving currently is "Something's not right. Output of hobbies
: [null,null,null]". So I'm wondering if something in the map() part of the code isn't right.
I've also tried replacing the concat() method with spread syntax, but the error I get then is that "list is not iterable." That makes me think it's not the map() method that is wrong.
I've tried doing something without map, but really don't know how else to drill down into each object by just using reduce(), so I don't think that's right either. Especially since this is a lesson on nested data.
I can't even list everything I've tried because I've been working on this for so long.
Can someone provide me with some clarification on what to focus on to fix this? The built-in feedback that the result is [null, null, null] isn't helping. Other than making me guess that my map() method is wrong, but I need something more to go on. Everything I've reviewed with that map() code makes this code look correct to me.
Any guidance is much appreciated. I know I will have to work more in-depth on array iteration at this point, as this course has been extremely challenging. Thank you!
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.hobbies)
.reduce((arr, list) => arr.concat(list), []);
1 Answer
Marcos Voltolini
42 PointsHi Lainey, you are super close! The only issue is that you are iterating through a nested data structure. The hobbies
list is not a field of customer
, but instead, personal
inside the customer
structure. You can access it via customer.personal
. So you just need to rewrite your mapping function:
.map(customer => customer.personal.hobbies)
Lainey Odette
18,618 PointsLainey Odette
18,618 PointsMarcos! You're my hero! I feel a little goofy for missing something small like that in the source data, but I can't tell you how much I appreciate you pointing that out! That actually makes me feel so much better, as it wasn't so much my understanding of the method, but more attention to detail. Maybe eating cake for lunch was a being a little hard on myself. :)
A sincere thank you!