screen_rotation
Copied to Clipboard
<html> <head> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <h3>Vue Js Get value from child Array with a given ID </h3> <table> <thead> <tr> <th>ID</th> <th>Name</th> </tr> </thead> <tbody> <tr v-for="child in selectedParent.children" :key="child.id"> <td>{{ child.id }}</td> <td>{{ child.name }}</td> </tr> </tbody> </table> </div> <script type="module"> const app = new Vue({ el: "#app", data() { return { parents: [ { id: 1, name: 'Parent 1', children: [ { id: 1, name: 'Child 1-1' }, { id: 2, name: 'Child 1-2' }, ], }, { id: 2, name: 'Parent 2', children: [ { id: 3, name: 'Child 2-1' }, { id: 4, name: 'Child 2-2' }, ], }, ], selectedParentId: 2, }; }, computed: { selectedParent() { return this.parents.find(parent => parent.id === this.selectedParentId); }, }, }); </script> <style scoped> #app { display: flex; flex-direction: column; align-items: center; justify-content: center; font-family: Arial, sans-serif; } table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; font-weight: bold; } tr:nth-child(even) { background-color: #f2f2f2; } tr:hover { background-color: #ddd; } </style> </body> </html>