当前位置:首页 > 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 实现录播播放 使用 video.js 实现 安装 video.js 和相关的 Vue 适配器: npm install video.js @videojs-player/vue 在 Vue…

vue实现下拉框删除

vue实现下拉框删除

Vue 实现下拉框删除功能 在Vue中实现下拉框的删除功能通常涉及以下步骤: 数据绑定与渲染 在Vue组件中定义数据数组用于存储下拉框选项: data() { return { op…

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构 V…

vue翻页实现

vue翻页实现

Vue翻页实现方法 基础分页组件实现 使用v-for和计算属性实现基础分页逻辑: <template> <div> <ul> <li…

proxy实现vue

proxy实现vue

使用 Proxy 实现 Vue 响应式系统 Vue 3 的响应式系统基于 JavaScript 的 Proxy API,通过代理对象实现对属性的拦截和依赖追踪。以下是实现的核心逻辑: 创建响应式对象…