1、Math.floor(Math.random() * array.length)

返回长度内的索引

eg:

changeLimit () {
  function getArrayItems(arr, num) {
    const temp_array = [];
    for(let index in arr) {
      temp_array.push(arr[index]);
    }
    const return_array = [];
    for (let i = 0; i<num; i++) {
      if(temp_array.length>0) {
        const arrIndex = Math.floor(Math.random()*temp_array.length);
        return_array[i] = temp_array[arrIndex];
        temp_array.splice(arrIndex, 1);
      } else {
        break;
      }
    }
    return return_array;
  }
  this.randomMovieList = getArrayItems(this.movieList, 5);
}

2、splice

temp_array.splice(arrIndex, 1),

删除temp_array中下标为arrIndex的那个元素

Math.floor(Math.random() * array.length),splice的更多相关文章

  1. Math.floor(Math.random()*3+1)

    Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and ret ...

  2. Number(),parseInt(),parseFloat(),Math.round(),Math.floor(),Math.ceil()对比横评

    首先,这些处理方法可分为三类. 1,只用来处理数字取整问题的:Math.round(),Math.floor(),Math.ceil(): 2,专门用于把字符串转化成数值:parseInt(),par ...

  3. js 中的 Math.ceil() Math.floor Math.round()

    alert(Math.ceil(25.9)); alert(Math.ceil(25.5)); alert(Math.ceil(25.1)); alert(Math.round(25.9)); ale ...

  4. 如何在一个页面后面随机跳转到多个链接地址Math.floor()和Math.random()

    点击一个标签随机跳转到多个链接地址,主要运用javascript中的Math.floor()和Math.random()方法 floor(x) 方法是向下去整数 参数为任意数值或表达式. floor( ...

  5. Math.round()、Math.ceil()、Math.floor()与Math.random()的区别?

    Math.round(x) 四舍五入 加上0.5向下取整 Math.round(1.5) 2 Math.round(-11.5) -11 Math.round(-11.2) -10 Math.ceil ...

  6. 转载:使用Math.floor和Math.random取随机整数

    Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and ret ...

  7. JavaScript中的内置对象-8--3.Math-Math对象的方法-min()- max()- ceil() - floor()- round()- abs(); Math对象的random()方法;

    JavaScript内置对象-3.Math(数值) 学习目标 1.掌握Math对象的方法: min() max() ceil() floor() round() abs() Math.min() 语法 ...

  8. Python标准库12 数学与随机数 (math包,random包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经在Python运算中看到Python最基本的数学运算功能.此外,math包 ...

  9. Java Math floor round ceil 函数

    public final class Math extends Object public static double floor(double a) public static long round ...

随机推荐

  1. jmeter静默压测+可视化

    静默压测自动化脚本auto_stress_test.sh #!/usr/bin/env bash export jmx_template="test2" export suffix ...

  2. sql server 2008 r2 直接下载地址,可用迅雷下载

    sqlserver 2008 r2 直接下载地址,可用迅雷下载 下载sqlserver 2008 r2 ,微软用了一个下载器,经过从下载器上,将他的地址全部用键盘敲了下来.最终的简体中文版地址如下: ...

  3. Linux基础命令之.命令

    . 点命令 .命令等同source 可以让配置文件被读到进程中,立刻生效

  4. c++ 获取当前时间周初凌晨时间戳(获取当前时间周一凌晨时间戳)

    UINT64 GetWeekBeginTime(){ time_t t; t = time(0); tm* t_tm = localtime(&t); t_tm->tm_hour = 0 ...

  5. 装java开发环境 报client/jvm.dll找不到

    jdk安装成功,明明有jvm.dll就是找不到,导致eclipse打不开.此时eclipse为64位,jdk为32位:查了好多方法,不起作用.最后将jdk换位64位问题解决. 所以,一般64位的系统建 ...

  6. DRF (学习第一部)

    目录 Web应用模式 API接口 RESTful API 规范 序列化 Django Rest_Framework 环境安装预与配置 序列化器 -Serializer Web应用模式 在开发web应用 ...

  7. mybatis逆向工程配置

    1.准备配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConf ...

  8. Ubuntu下创建apt源

    1. 下载所需安装文件 sudo apt-get install soft name  安装并保存安装文件 或者 sudo apt-get source soft name      只下载安装文件 ...

  9. Boxing

    测试自动装箱和自动拆箱,意思是运行的时候编译器帮我们加了两个代码: public class AutoBoxingandUnBoxing { public static void main(Strin ...

  10. NB-IOT覆盖范围有多大 NB-IOT的强覆盖是怎么实现的

    NB-IoT技术自出现以来就以其强大的覆盖范围和通讯距离长而受到广泛的欢迎,发展到现在已经成为万物互联网络中的一个重要分支.那么NB-IoT覆盖范围到底有多大,是怎么来衡量其覆盖能力? 强大的覆盖范围 ...