I want to access data from a prop that has an array within an array, I have to type tons of v-for
statements to access the data.
Currently I have to cycle through the spaces
array (check if their id matches with the one I want) and use an v-if
statement to output only the one I want, then I repeat that cycle for rooms
(check ids again).
Is there a way to filter
through arrays within arrays? Without these for/if statements? I've been reading into filters but haven't been able to figure it out for what I need.
Vue file:
<v-flex hidden-sm-and-down sm4 lg4 class="sidebarSticky">
<v-layout v-for="space in spaces"
v-if="space.id === selectedSpace"
:key="space.id"
>
<!-- 1 room details -->
<v-flex v-for="room in space.rooms"
v-if="selectedRoom === room.id"
v-if="selectedRoom "
:key="room.id">
<v-card>
<v-card-text>
<!-- room name -->
<h2 class="sidebarRoomName"> {{ room.name }} </h2>
<!-- description -->
<p> {{ room.description }} </p>
</v-card-text>
</v-card>
</v-flex>
</v-layout>
</v-flex>
My Data:
new Vue({
el: '#app',
data: () => ({
selectedSpace: 0;
selectedRoom: 1,
spaces: [
{
id: 0,
name: 'House in Amsterdam',
rooms: [
{
id: 0,
name: 'My epic living room'
},
{
id: 1,
name: 'My epic room'
}
]
},
{
id: 5,
name: 'House in Paris',
rooms: [
{
id: 0,
name: 'My epic bathroom'
}
]
}
]
})
})
The code above might seem very simple, but I'm working with a lot more data and it can be super difficult to work with.