当前位置:首页 > VUE

vue实现随机分组功能

2026-01-22 04:33:07VUE

实现随机分组功能的方法

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

准备数据

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

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

随机打乱数组

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

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))
    }
  }
}

固定组数而非每组人数

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

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实现

Vue 实现订单功能 数据模型设计 订单功能通常需要设计以下数据结构: // 订单数据结构 const order = { id: '', // 订单ID userId:…

vue实现aop切面

vue实现aop切面

Vue 实现 AOP 切面编程的方法 在 Vue 中实现 AOP(面向切面编程)可以通过多种方式,以下是几种常见的实现方法: 使用 Vue 混入(Mixins) Vue 的混入机制可以用于实现横切关…

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name…

eventbus vue实现

eventbus vue实现

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

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue tag实现

vue tag实现

Vue 标签实现方法 在 Vue 中实现标签功能可以通过多种方式完成,常见场景包括动态标签生成、标签输入框、标签管理等。以下是几种典型实现方案: 动态标签列表渲染 使用 v-for 指令渲染标签数组…