Nested v-for Loops
Provide an example of rendering a list with nested arrays using nested v-for loops.
<template>
<div>
<!-- Outer loop for parent array -->
<ul v-for="(item, index) in items" :key="index">
<li>
{{ item.name }}
<!-- Nested loop for child array -->
<ul>
<li v-for="subItem in item.children" :key="subItem.id">
{{ subItem.name }}
</li>
</ul>
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
// Sample data with nested arrays
items: [
{
name: 'Item 1',
children: [
{ id: 1, name: 'Sub Item 1-1' },
{ id: 2, name: 'Sub Item 1-2' }
]
},
{
name: 'Item 2',
children: [
{ id: 3, name: 'Sub Item 2-1' },
{ id: 4, name: 'Sub Item 2-2' }
]
}
// Add more items if needed
]
};
}
};
</script>
This Vue.js component template and script illustrate rendering a list with nested arrays using nested v-for loops. The outer loop iterates over items, creating an unordered list, and the inner loop iterates over each item's children, creating another unordered list within each list item.