数组的随机抽取...

operateArray.js

// 目标数组中 随机抽取 新数组
export const getRandomArray = function (arr, num) {
  let out = []
  let n = arr.length > num ? num : arr.length
  while (out.length < n) {
    let temp = parseInt(Math.random() * arr.length)
    out = [...out, ...arr.splice(temp, 1)]
  }
  return out
}
复制代码

使用

import { getRandomArray } from '../utils/operateArray'

getRandomArray([1,2,3,4,5], 3) // [4, 1, 2]
复制代码

转载于:https://juejin.im/post/5cb82fa1e51d456e660d44a8

你可能感兴趣的:(数组的随机抽取...)