+ 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);
2 odpowiedzi
+ 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], []);
+ 3
Thanks for the help