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

Wouldn't 'Array.flat()' work instead of .reduce and .map? attempted to do it but didn't work

const users = [
    {
        name: 'Samir',
        age: 27,
        favoriteBooks: [
            {title: 'The Iliad'},
            {title: 'The Brothers Karamazov'}
        ]
    },
    {
        name: 'Angela',
        age: 33,
        favoriteBooks: [
            {title: 'Tenth of December'},
            {title: 'Cloud Atlas'},
            {title: 'One Hundred Years of Solitude'}
        ]
    },
    {
        name: 'Beatrice',
        age: 42,
        favoriteBooks: [
            {title: 'Candide'}
        ]
    }
];




//This didnt seem to work//
let iii ;

iii = users.flat(3);

console.log(iii);


//this code was for the challenge **** Please look above
// const movieTitles = users.filter( onlyTitles=> onlyTitles.favoriteBooks
//     .map( books=> books.favoriteBooks))
//     .reduce((lib, book) => [...lib,...book.favoriteBooks], [] );
//
//
// console.log(movieTitles);

Note: it would say that '.flat is not a function', I also tried with the depth being 'infinity'

1 Answer

Steven Parker
Steven Parker
231,172 Points

The test library might not include "flat". But access to the function wouldn't help, because this array is already "flat".

The task here is to get attributes from the objects of the array, which is not what "flat" does.

Thanks, looks like I got confused with the concept of 'flat'. Thanks again