我尝试做的只是过滤商店列表,将其存储在名为grapefruitStores的新变量对象中,然后用JavaScript将其打印到控制台。我允许使用filter,map和reduce。这就是我到目前为止所拥有的:
const stores = [{
name: "Kents",
foods: [
{name: 'bagels', type: 'grain'},
{name: 'bread', type: 'grain'},
{name: 'cereal', type: 'grain'},
{name: 'milk', type: 'dairy'},
]
},{
name: "Maceys",
foods: [
{name: 'bagels', type: 'grain'},
{name: 'bread', type: 'grain'},
{name: 'cereal', type: 'grain'},
{name: 'grapefruit', type: 'fruit'},
{name: 'milk', type: 'dairy'},
]
}];
//Filtering code
function sellsGrapefruit(stores) {
return stores.foods.name === 'grapefruit'; //Don't think this is correct.
}
var grapefruitStores = store.filter(sellsGrapefruit);
console.log(grapefruitStores);
因此,只有Maceys的对象信息应该打印到控制台,因为Maceys出售葡萄柚而Kents不出售。一个空的数组/对象继续打印到屏幕,我不知道为什么。我在这做错了什么?任何帮助将不胜感激。
答案 0 :(得分:4)
您可以使用Array.some()
:
Caused by: org.jboss.weld.exceptions.IllegalProductException: WELD-000053: Producers cannot declare passivating scope and return a non-serializable class: Producer for Producer Method [FacesContext] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces @ViewScoped public br.com.dropper.web.factory.FacesContextFactory.getFacesContext()] declared on Managed Bean [class br.com.dropper.web.factory.FacesContextFactory] with qualifiers [@Any @Default]
at br.com.dropper.web.factory.FacesContextFactory.getFacesContext(FacesContextFactory.java:16)
答案 1 :(得分:0)
因此使用filter
方法非常简单。这样做包括数组中的元素,具体取决于传递给它的函数的布尔返回值。你几乎和你发布的内容一样,但食物是一个对象数组,每个对象都有一个name属性,你当前正在调用商店的(不存在的)name属性。
因此,对于这个例子,通过工作迭代,一个可行的解决方案是:
var grapefruitStores = stores.filter(function(store) {
for (var i = 0; i < store.foods.length; i++) {
if (store.foods[i].name === 'grapefruit') {
return true;
}
}
});
答案 2 :(得分:0)
您的过滤器逻辑存在一些缺陷。您的算法正在检查foods
数组的stores
属性。它应该检查foods
数组中每个元素的stores
属性。试试这个:
//Filtering code
function sellsGrapefruit(store) {
for (var i in store.foods) {
var food = store.foods[i];
if (food.name === 'grapefruit')
return true;
}
return false;
}
这是一个有效的例子:
答案 3 :(得分:0)
Foods数组嵌套在stores数组中的对象中,因此您必须使用filter
两次才能获得所需的结果:
const stores = [{
name: "Kents",
foods: [
{name: 'bagels', type: 'grain'},
{name: 'bread', type: 'grain'},
{name: 'cereal', type: 'grain'},
{name: 'milk', type: 'dairy'}
]
},{
name: "Maceys",
foods: [
{name: 'bagels', type: 'grain'},
{name: 'bread', type: 'grain'},
{name: 'cereal', type: 'grain'},
{name: 'grapefruit', type: 'fruit'},
{name: 'milk', type: 'dairy'}
]
}];
function sellsGrapefruit(store) {
var x = store.foods.filter(function(i) {
return i.name === 'grapefruit';
});
return x.length > 0;
}
var grapefruitStores = stores.filter(sellsGrapefruit); // typo error: store -> stores
console.log(grapefruitStores[0]);
答案 4 :(得分:0)
由于您只能使用map
,reduce
,filter
。我采用了这种方法。
var grapeStores = stores.filter(function (store) {
return store.foods.filter(function (food) {
return food.name === 'grapefruit';
}).length;
});
console.log('grapeStores', grapeStores);
记录
grapeStores [ { name: 'Maceys',
foods: [ [Object], [Object], [Object], [Object], [Object] ] } ]