screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html lang="en"> <head> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <h2>Vue Js Get Multiple Random Element from Array</h2> <ul> <li v-for="item in selectedItems" :key="item">{{ item }}</li> </ul> <button @click="selectItems">Select Items</button> </div> <script type="module"> const app = new Vue({ el: "#app", data() { return { items: ['apple', 'banana', 'orange', 'pear', 'kiwi', 'mango'], numItems: 3, selectedItems: [] }; }, methods: { selectItems() { let remainingItems = [...this.items]; this.selectedItems = []; for (let i = 0; i < this.numItems; i++) { let randomIndex = Math.floor(Math.random() * remainingItems.length); let selectedItem = remainingItems.splice(randomIndex, 1)[0]; this.selectedItems.push(selectedItem); } } }, mounted() { this.selectItems() } }) </script> <style scoped> #app { margin: 20px; padding: 20px; border: 1px solid #ccc; } h2 { font-size: 24px; margin-bottom: 10px; } ul { list-style: none; margin: 0; padding: 0; } li { font-size: 18px; margin-bottom: 5px; } button { background-color: #007bff; color: #fff; border: none; border-radius: 4px; padding: 10px 20px; font-size: 18px; cursor: pointer; transition: background-color 0.3s ease; } button:hover { background-color: #0069d9; } </style> </body> </html>