First time posting a question. Hunted through stackoverflow for the answer but didn't find it. If I missed it, and this is a duplicate question, please comment a link and I'll delete this! (newbie here)
So basically, I have an array them_dogs
containing objects. I am trying to write a function that accepts an array and an object value, say 'tennis ball'
. This function will then look through each item in the array and check to see if the value 'tennis ball'
is present. If so, it will return the item name.
var them_dogs = [{
name: 'Henry',
age: 0.5,
breed: 'Aussie',
food: 'kibble',
toys: ['tennis ball', 'chew toy'],
picture: 'http://rubyriverminiaustralianshepherds.com/wp-content/uploads/aussie-puppy-for-sale-940x412.jpg'
}, {
name: 'Tilly',
age: 5,
breed: 'Mutt',
food: 'kibble',
toys: ['bone', 'kong', 'squeaky toy'],
picture: 'http://www.dogchannel.com/images/zones/top_promo/excited-mixed-breed.jpg'
}, {
name: 'Apollo',
age: 10,
breed: 'Labrador',
food: 'absolutely anything',
toys: ['old sock', 'other old sock', 'more old socks'],
picture: 'http://media.cmgdigital.com/shared/img/photos/2014/08/01/5a/66/LadyLabrador.jpg'
}]
Here is the function
var findDogByToy = function (arr, toyname) {
arr.forEach (function (item) {
if (item.toys === toyname) {
return item.name;
}
})
}
findDogByToy(them_dogs, 'tennis ball');