I got it to return an array containing the array of puppies stored in the object dogs{ }
But for some reason It wont accept it when I run the function against the tests.
Description of function I need to create:
"
This function takes an array of dogs in the format:
[
{breed: ‘Labrador’, puppies: [‘Fluffy’, ‘Doggo’, ‘Floof’] },
{breed: ‘Rottweiler’, puppies: [‘Biscuits’, ‘Mary’] }
]
It should return an array of all the puppies from all the dogs:
[‘Fluffy’, ‘Doggo’, ‘Floof’, ‘Biscuits’, ‘Mary’]
"
Here Is what I made
function collectPuppies (dogs) {
console.log("dogs : " + dogs)
console.log("puppies : " + dogs.puppies)
return dogs.puppies;
}
Here is what I receive when I test it
dogs is an array, not an object
therefore to access any objects inside dogs, you will need a square bracket notation like
dogs[0] or dogs[1]
So I want to add each objects puppies to one big puppy array. But I dunno why it wont read the property of the object when I tried the following.
function collectPuppies (dogs) {
var arrayOfPuppers;
function objectDogs (dog)
{
arrayOfPuppers.push(dog.puppies)
console.log(dog.puppies)
}
dogs.forEach(objectDogs)
console.log(arrayOfPuppers)
}
collectPuppies([
{ breed: 'Dalmation', puppies: [] },
{ breed: 'Rottweiler', puppies: [] }
])
This is your code above.
Do you see the square brackets at the start and end of the list? They look like this [ and ]
These indicate an array , not an object.
Therefore to access the array, you need something with square brackets like this:
dogs[0].puppies //this gives the first object’s puppies array