JavaScript JavaScript Array Iteration Methods Combining Array Methods Nested Data

victor escarcega
victor escarcega
8,667 Points

Trying to flatten an object

not sure what I am missing here

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.map( passtime => passtime.hobbies))
  .reduce( (accumulator, currentValue) => [...accumulator, ...currentValue], []);

1 Answer

Steven Parker
Steven Parker
207,988 Points

There isn't anything defined with the name "passtime"; and "personal" is an object, so it doesn't have a "map" method.

But only only need to use "map" once in this challenge anyway.