All Im trying to do is filter a list of stores store it in a
All I\'m trying to do is filter a list of stores, store it in a variable of stores that sell grapefruit, and print it to the console in JavaScript. What am I doing wrong? I guess I\'m probably confused on accessing array elements/objects inside of other array elements/objects. Any help would be appreciated. Here\'s what I have so far:
<!DOCTYPE html>
<html>
<body>
<h1>My Program</h1>
<script>
const store = [{
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) {
return store.foods.name === \'grapefruit\';
}
var grapefruitStores = recipes.filter(sellsGrapefruit);
console.log(grapefruitStores);
</script>
</body>
</html>
Solution
here the modified code you are just making syntax error with every type element.
though i tried to run it and it gave \"JavaScript error: Uncaught ReferenceError: recipes is not defined on-line..\" this is because i don\'t have access to full code so i tried to test it with constructor STORE and it worked just fine display empty error(doesnt contain any value at this time) in console.
happy to help you ask agin if you have more queries
DOCTYPE html>
<html>
<body>
<h1>My Program</h1>
<script>
const store = [{
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) {
return store.foods.name === \'grapefruit\';
}
var grapefruitStores = recipes.filter(sellsGrapefruit);
console.log(grapefruitStores);
</script>
</body>
</html>

