Math.floor(Math.random() * array.length),splice
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的更多相关文章
- Math.floor(Math.random()*3+1)
Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and ret ...
- Number(),parseInt(),parseFloat(),Math.round(),Math.floor(),Math.ceil()对比横评
首先,这些处理方法可分为三类. 1,只用来处理数字取整问题的:Math.round(),Math.floor(),Math.ceil(): 2,专门用于把字符串转化成数值:parseInt(),par ...
- 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 ...
- 如何在一个页面后面随机跳转到多个链接地址Math.floor()和Math.random()
点击一个标签随机跳转到多个链接地址,主要运用javascript中的Math.floor()和Math.random()方法 floor(x) 方法是向下去整数 参数为任意数值或表达式. floor( ...
- 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 ...
- 转载:使用Math.floor和Math.random取随机整数
Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and ret ...
- 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() 语法 ...
- Python标准库12 数学与随机数 (math包,random包)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经在Python运算中看到Python最基本的数学运算功能.此外,math包 ...
- Java Math floor round ceil 函数
public final class Math extends Object public static double floor(double a) public static long round ...
随机推荐
- docker-docker-compose 安装
1.安装docker-compose(官网:https://github.com/docker/compose/releases) 安装: curl -L https://github.com/doc ...
- java 调用WebService【转】
1.客户单编程方式(和第一种方式一样) //文件名:HelloService.java import javax.jws.WebMethod; import javax.jws.WebParam; i ...
- python GDAL 读写shp文件
gdal包用于处理栅格数据,ogr用于处理矢量数据. 1 #!C:\Program Files\pythonxy\python\python.exe 2 #-*- coding:gb2312 -*- ...
- 【事件中心 Azure Event Hub】在Linux环境中(Ubuntu)安装Logstash的简易步骤及配置连接到Event Hub
在文章([事件中心 Azure Event Hub]使用Logstash消费EventHub中的event时遇见的几种异常(TimeoutException, ReceiverDisconnected ...
- JavaScript的9大排序算法详解
一.插入排序 1.算法简介 插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入. ...
- flutter_bloc使用解析---骚年,你还在手搭bloc吗!
前言 首先,有很多的文章在说flutter bloc模式的应用,但是百分之八九十的文章都是在说,使用StreamController+StreamBuilder搭建bloc,提升性能的会加上Inher ...
- 类型“DbContext”在未引用的程序集中定义。必须添加对程序及“EntityFramework,Version=6.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”的引用。using语句中使用的类型必须可隐式转换为”System.IDisposable
其他层引用Model层的ef模型时会发生这个错误 解决方法: 在你要使用EF模型的层下点击添加引用 然后点击浏览 找到Model层文件下的bin>debug文件 引用这两个dll文件 如 ...
- 微服务nacos服务注册与发现
一,以上一篇为基础 微服务从nacos配置中心获得配置信息 给service1, service2添加依赖 <dependency> <groupId>com.alibaba. ...
- vue组件使用name属性来生成递归组件
先来个简单的数据 1 lists = [{ 2 id: 1, 3 title: '第一层', 4 children: [{ 5 id: 3, 6 title: '第二层', 7 children: [ ...
- [C#.NET 拾遗补漏]10:理解 volatile 关键字
要理解 C# 中的 volatile 关键字,就要先知道编译器背后的一个基本优化原理.比如对于下面这段代码: public class Example { public int x; public v ...