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,217 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,656 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!