I have a Vue page to display recipes by category:
<template>
<div class="recipes-page">
<div class="recipes" v-for="r in categoryRecipes" :key="r.id">
<span>{{ r.title }}</span>
</div>
</div>
</template>
<script>
export default {
computed: {
loadedRecipes() {
return this.$store.getters.loadedRecipes;
},
loadedCategories() {
return this.$store.getters['categories/loadedCategories'];
},
categoryRecipes() {
let categorized = [];
this.$store.getters['categories/loadedCategories'].forEach(cat => {
this.$store.getters.loadedRecipes.forEach(recipe => {
if (cat.id in Object.keys(recipe.categories)) { // doesnt work
console.log("Recipe has the category with id: " + cat.id);
categorized.push(recipe);
}
});
});
return categorized;
}
}
};
</script>
In the data structure we have categories and recipes:
Categories:
[
{
name: 'Juices',
recipes: {
'-L_Pg_BbwMYaGQjI2ejd': true
},
id: 'juices'
},
{
name: 'Misc',
recipes: {
'-L_Pg_BbwMYaGQjI2ejd': true,
'-L_PjcLaCvhZr9nb6wbh': true
},
id: 'misc'
}
]
Recipes:
[
{
author: 'Ian K',
categories: {
juices: true,
misc: true
},
citation: 'none',
cookTime: 'n/a',
created: '2019-03-08T00:27:09.774Z',
description: 'How to make Orange Juice from frozen concentrate.',
directions: 'Mix frozen orange juice and water in a pitcher.',
featured: true,
id: '-L_Pg_BbwMYaGQjI2ejd',
prepTime: '1 min',
starCount: 5,
thumbnail: 'https://baconmockup.com/420/420',
title: 'Orange Juice from Concentrate',
totalTime: '1 min',
updated: '2019-03-08T00:27:09.774Z',
yield: '8 Cups'
},
...
]
The problem is that the if statement never returns true, even though the cat.id does exist in the recipe categories keys. Does anyone know why checking the keys array contents does not work?
I'd also like to know if chaining iteration like this will have a negative performance impact, and if there is a better/more convenient way to go about it?
recipe.categoriesis an array and not an object?