当前位置:首页 > VUE

vue实现随机分组功能

2026-01-22 04:33:07VUE

实现随机分组功能的方法

在Vue中实现随机分组功能可以通过以下步骤完成。这里假设需要将一组人员随机分成若干小组,每组人数可能固定或不固定。

准备数据

定义一个数组存储待分组的人员名单,并设置每组的人数或组数。

data() {
  return {
    people: ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十'],
    groupSize: 2, // 每组2人
    groups: []
  }
}

随机打乱数组

使用Fisher-Yates洗牌算法对数组进行随机排序。

vue实现随机分组功能

methods: {
  shuffleArray(array) {
    const newArray = [...array]
    for (let i = newArray.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1))
      ;[newArray[i], newArray[j]] = [newArray[j], newArray[i]]
    }
    return newArray
  }
}

分组逻辑

根据打乱后的数组创建分组。

methods: {
  createGroups() {
    const shuffled = this.shuffleArray(this.people)
    this.groups = []

    for (let i = 0; i < shuffled.length; i += this.groupSize) {
      this.groups.push(shuffled.slice(i, i + this.groupSize))
    }
  }
}

固定组数而非每组人数

如果需要指定组数而非每组人数,可以这样修改:

vue实现随机分组功能

methods: {
  createGroupsByNumber(numGroups) {
    const shuffled = this.shuffleArray(this.people)
    this.groups = Array(numGroups).fill().map(() => [])

    shuffled.forEach((person, index) => {
      const groupIndex = index % numGroups
      this.groups[groupIndex].push(person)
    })
  }
}

模板展示

在模板中展示分组结果。

<template>
  <div>
    <button @click="createGroups">随机分组</button>
    <div v-for="(group, index) in groups" :key="index">
      <h3>第 {{ index + 1 }} 组</h3>
      <ul>
        <li v-for="person in group" :key="person">{{ person }}</li>
      </ul>
    </div>
  </div>
</template>

完整组件示例

将上述代码整合成一个完整的Vue组件。

<template>
  <div>
    <div>
      <label>每组人数:</label>
      <input type="number" v-model.number="groupSize" min="1" :max="people.length">
      <button @click="createGroups">随机分组</button>
    </div>
    <div v-for="(group, index) in groups" :key="index">
      <h3>第 {{ index + 1 }} 组 ({{ group.length }}人)</h3>
      <ul>
        <li v-for="person in group" :key="person">{{ person }}</li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      people: ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十'],
      groupSize: 2,
      groups: []
    }
  },
  methods: {
    shuffleArray(array) {
      const newArray = [...array]
      for (let i = newArray.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1))
        ;[newArray[i], newArray[j]] = [newArray[j], newArray[i]]
      }
      return newArray
    },
    createGroups() {
      const shuffled = this.shuffleArray(this.people)
      this.groups = []

      for (let i = 0; i < shuffled.length; i += this.groupSize) {
        this.groups.push(shuffled.slice(i, i + this.groupSize))
      }
    }
  }
}
</script>

注意事项

  • 确保分组逻辑不会导致最后一组人数过少
  • 可以添加输入验证防止无效的组大小
  • 对于大型数据集,考虑使用更高效的洗牌算法
  • 可以扩展功能支持不同的分组策略(如按性别、技能等平衡分组)

标签: 功能vue
分享给朋友:

相关文章

vue实现多级表头

vue实现多级表头

Vue 实现多级表头的方法 使用 el-table 组件 Element UI 的 el-table 组件支持多级表头的实现,通过嵌套 el-table-column 即可完成。 <el-t…

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-lin…

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template>…

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现轮询

vue实现轮询

实现轮询的基本方法 在Vue中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: data…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独…