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 JavaScript Array Iteration Methods Combining Array Methods Nested Data

victor escarcega
victor escarcega
8,667 Points

not sure where to go from here

what am I missing on this reduce function?

app.js
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 = customers
  .map(i => i.hobbies)
  .reduce((arr, items) => [ ...arr, ...items ], []);

// hobbies should be: ["Bicycling", "Camping", "Guitar", "Reading", "Gardening", "Comics", "Chess", "Legos"]
// Write your code below

1 Answer

Hey Victor. You are not the only person having trouble with this challenge. I actually just answered another question about the same one, and both you and this other student made the mistake of thinking you need to use map, which you don't. The Array Iteration Methods course does emphasize a lot that you can combine array iteration methods to perform complex data manipulations and I think students start to get it in their heads that they need to always be combining these methods. But I can tell you as someone who has built a bunch of apps over the past couple years, I don't actually use combinations of array methods as often as you would think. It's good to know how because in interviews you might be asked to solve tricky code challenges and require elegant and efficient solutions, but practical programming is often not as hard as the programming you're asked to do in interviews.

First, get rid of the map call. reduce does not need you to map over your data to simplify it first. reduce can easily deal with complex data structures. Second, use the spread operator to add each item in the personal.hobbies array to the accumulator. So your code will look like this:

hobbies = customers.reduce((arr, items) => [ ...arr, ...items.personal.hobbies ], []);

Since items is your accumulator, I would rename it to something like curr or currentItem to help you remember the role of each parameter in reduce, but you don't have to if you don't think it will help you.

As you can see, your code was really similar to mine, so you were definitely on the right track. Good luck!

victor escarcega
victor escarcega
8,667 Points

Thank you so much for taking the time to provide me with such a full answer. It is very helpful and will help me not overthink it in the future.Thank you!