I am trying to accomplish the following...
Given the array cakes as:
const cakes = [
{id: 1, name: 'chocolate'},
{id: 2, name: 'strawberry'},
{id: 3, name: 'mint'}
]
And the array 'favorite cakes' as:
const favCakes = [
{id: 1, cakeId: 1},
{id: 2, cakeId: 2}
]
I want to log FAVORITE CAKES and NOT FAVORITE CAKES on console so I would expect a result like this:
FAVORITE CAKES: 'chocolate'
FAVORITE CAKES: 'strawberry'
NOT FAVORITE CAKES: 'mint'
But it does't work as expected as you can see from the snippet below. What am I doing wrong? I appreciate any help.
const cakes = [
{id: 1, name: 'chocolate'},
{id: 2, name: 'strawberry'},
{id: 3, name: 'mint'}
]
// favorite cakes for logged in user
const favCakes = [
{id: 1, cakeId: 1},
{id: 2, cakeId: 2}
]
for (const cake of cakes) {
for (const fav of favCakes) {
if (cake.id === fav.cakeId) {
console.log('FAVORITE CAKE: ', cake.name )
}
else if (cake.id !== fav.cakeId) {
console.log('NOT FAVORITE CAKE: ', cake.name )
}
}
}