Hi I am 99% there with the code below but I need this array of objects to return:- 'Bear', 'Minu', 'Basil', 'Hamish' instead it returns Bear,MinuBasil,Hamish any ideas?
gatherPets([{ name: 'Malcolm', pets: ['Bear', 'Minu'] },{ name: 'Caroline', pets: ['Basil',
'Hamish'] },]);
function gatherPets(people) {
let newArray=[];
for (let i=0; i <=people.length; i++ ) {
newArray = newArray +people[i].pets
}
return (newArray)
}
I hope this code helping you
array.flatMap(o=>o.pets)
You could use the Array#flatMap method for this.
export function gatherPets(pople){
return people.flatMap(p => p.pets);
}
try this
gatherPets([{ name: 'Malcolm', pets: ['Bear', 'Minu'] },{ name: 'Caroline', pets: ['Basil',
'Hamish'] },]);
function gatherPets(people) {
let newArray=[];
for (let i=0; i <people.length; i++ ){
newArray.push(...people[i].pets);
}
return (newArray)
}