Help me solve these question to just output the hobbies | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Help me solve these question to just output the hobbies

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.hobbies.map(hobby => hobby.hobbies)) .reduce ((arr, hobbies) => [...arr, ...hobbies], []); //console.log(hobbies);

3rd Jan 2019, 12:32 PM
David Danso
David Danso - avatar
2 Answers
+ 2
hobbies = customers .map(customer => customer.personal.hobbies) .reduce ((arr, hobbies) => [...arr, ...hobbies], []); or just one reduce: hobbies = customers .reduce ((arr, customer) => [...arr, ...customer.personal.hobbies], []);
3rd Jan 2019, 7:53 PM
Andreas
Andreas - avatar
+ 3
Thanks for the help
3rd Jan 2019, 9:31 PM
David Danso
David Danso - avatar