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. 标签中colgroup的属性

    如需对全部列应用样式,<colgroup> 标签很有用,这样就不需要对各个单元和各行重复应用样式了. <colgroup> 标签只能在 table 元素中使用. 可选的属性 属 ...

  2. c++数组的替代品

  3. MongoDB添加认证

    创建用户管理员 在admin数据库中,添加具有该userAdminAnyDatabase角色的用户 .根据需要为此用户添加其他角色. 注意:创建用户的数据库(在此示例中为 admin)是用户的身份验证 ...

  4. java安全编码指南之:线程安全规则

    目录 简介 注意线程安全方法的重写 构造函数中this的溢出 不要在类初始化的时候使用后台线程 简介 如果我们在多线程中引入了共享变量,那么我们就需要考虑一下多线程下线程安全的问题了.那么我们在编写代 ...

  5. pyside2安装避坑

    cmd 输入 pip install PySide2 官方下载太慢 清华源: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pysid ...

  6. Spring Boot + Druid 多数据源绑定

    date: 2019-12-19 14:40:00 updated: 2019-12-19 15:10:00 Spring Boot + Druid 多数据源绑定 版本环境:Spring Boot 2 ...

  7. UWP仿网易云音乐之1-TitleBar

    首先,创建一个UWP的项目.我使用的是Visual Studio 2017 社区版. 如图,我们将项目命名为UWP-Music. 现在我们先标题栏的配色调整与网易云音乐一致. 我们先分析一下标题栏,默 ...

  8. 个人笔记docker

    启动docker sudo systemctl start docker.service   暂停docker sudo systemctl stop docker.service   运行docke ...

  9. linux修改ssh远程端口22

    建议先查看redhat的release版本,CentOS 7的启动服务不同: # more /etc/redhat-release 正文: 如题: 在此前,建议先查看redhat的release版本, ...

  10. [C#.NET 拾遗补漏]11:最基础的线程知识

    线程的知识太多,知识点有深有浅,往深的研究会涉及操作系统.CUP.内存,往浅了说就是一些语法.没有一定的知识积累,很难把线程的知识写得全面,当然我也没有这个能力.所以想到一个点写一个点,尽量总结一些有 ...