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 trial

JavaScript JavaScript Array Iteration Methods Combining Array Methods Nested Data

Using the reduce method

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.reduce((arr,arrf)=> [...arr, arrf.personal.[...hobbies],[]);

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.reduce((arr,arrf)=> [...arr, arrf.personal.[...hobbies],[]);

3 Answers

Steven Parker
Steven Parker
229,644 Points

You don't need bracket notation to access the "hobbies" property, that's actually the name of the property.

Also, you need to position your scatter operator at the beginning of the fully qualified name.

And you need to assign "hobbies", the "reduce" should be applied to the "customers" array.

1-hobbies = customers.reduce((arr,arrf)=> [...arr, arrf.personal...hobbies,[]); 2-hobbies = customers.reduce((arr,arrf)=> [...arr, arrf.personal....hobbies,[]);

Thank you so much !!!

Is this right? hobbies = customers.reduce((arr,arrf)=> [...arr, arrf.personal.hobbies...,[]);

Steven Parker
Steven Parker
229,644 Points

The scatter operator goes in front of the term, like you have on the first one.

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

Steven Parker
Steven Parker
229,644 Points

I mean it should go in front of the entire term (not just the property name). Plus it looks like you're missing a closing bracket ("]").

amz baz
amz baz
2,053 Points

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