第一次发帖提问。通过stackoverflow搜索答案,但没有找到它。如果我错过了,这是一个重复的问题,请评论一个链接,我将删除它! (新手在这里)
基本上,我有一个包含对象的数组them_dogs
。我正在尝试编写一个接受数组和对象值的函数,比如'tennis ball'
。然后,此函数将查看数组中的每个项目,并检查值'tennis ball'
是否存在。如果是这样,它将返回项目名称。
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'
}]
这是函数
var findDogByToy = function (arr, toyname) {
arr.forEach (function (item) {
if (item.toys === toyname) {
return item.name;
}
})
}
findDogByToy(them_dogs, 'tennis ball');
答案 0 :(得分:2)
正如其他人所提到的,使用forEach来解决这个问题是一种天真的方法,但是对于初学者来说,这些解释可能对您的理解有用:
function findDogByToy(array, toy) {
// loop across each item in the array
var result;
array.forEach(function(dog) {
// check that item (which is an object) to see if toy is in there
dog.toys.forEach(function(item) {
//if it is, then return item name
if (item === toy) {
result = dog.name;
}
});
});
return result;
}
答案 1 :(得分:1)
forEach
而不是every
。 every
更清晰,因为您不打算修改任何元素。而且,突破forEach
更难。请参阅how to stop Javascript forEach? indexOf
检测toyname
数组中是否存在toys
findDogByToy
需要实际返回值以使调用者受益
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'
}];
var findDogByToy = function(arr, toyname) {
var name;
arr.every(function(item) {
if (item.toys.indexOf(toyname) > -1) {
name = item.name;
return false;
}
});
return name;
}
console.log(findDogByToy(them_dogs, 'tennis ball'));

答案 2 :(得分:1)
我想你可能想要使用filter()和map()而不是forEach()。这样,如果您有多个名称,您将获得一系列名称。
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'
}]
var findDogByToy = function (arr, toyname) {
return arr.filter (function (item) {
return item.toys.indexOf(toyname) > -1;
}).map( function(o){ return o.name; });
}
console.log(findDogByToy(them_dogs, 'tennis ball'));

其他选项,只需使用简单的for循环并推送到数组。