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

Melissa Benton
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Melissa Benton
Front End Web Development Techdegree Graduate 17,230 Points

Still not getting the right array.

I'm still not understanding how to return the array so each item is a string. Right now they are all one string. Will someone tell me how to fix my code so I can move on, please? How do I make hobbyArr an accumulator?

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.reduce((hobbyArr, user) => [hobbyArr + user.personal.hobbies], []);

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey Melissa Benton 👋

Currently you're iterating over each of the customers and using the addition operator on the hobbies arrays. When using the addition operator on arrays like that you'll end up with a string instead of an array of strings.

This is basically what is happening in your current line of code:

hobbies = [] + ["Bicycling", "Camping"] + ["Guitar", "Reading", "Gardening"] + ["Comics", "Chess", "Legos"]

console.log(hobbies)
// "Bicycling,CampingGuitar,Reading,GardeningComics,Chess,Legos"

When you want to combine arrays with each other you'll want to be using the .concat method instead. This will make sure that the entries in each of the arrays will be combined into a single array:

hobbies = customers.reduce((acc, customer) => acc.concat(customer.personal.hobbies), []);

console.log(hobbies)
// ['Bicycling', 'Camping', 'Guitar', 'Reading', 'Gardening', 'Comics', 'Chess', 'Legos']

Alternatively you could use the spread operator to accomplish this:

hobbies = customers.reduce((acc, customer) => [...acc, ...customer.personal.hobbies], []);

console.log(hobbies)
// ['Bicycling', 'Camping', 'Guitar', 'Reading', 'Gardening', 'Comics', 'Chess', 'Legos']

Hope this helps 🙂