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

Jenniffer Hernandez
seal-mask
.a{fill-rule:evenodd;}techdegree
Jenniffer Hernandez
Full Stack JavaScript Techdegree Student 11,662 Points

Using 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??

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 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

hobbies = 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.

Sean T. Unwin
Sean T. Unwin
28,660 Points

As 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 for hobbies (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]

Great attempt and you'll get it with some refactoring!