码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript Snippet - Array【原】 2016-2-19

时间:2016-04-11 18:42:27      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

在特定范围里获得一个随机数
var x = Math.floor(Math.random() * (max - min + 1)) + min;

 

 

随机返回数组的一个元素
function arrayRandomValue(arr) {
    return arr[Math.floor(Math.random() * arr.length)];
}
 
var a = [1, 2, 3, 3, 4, 5, 6, 7, 8, 9];
arrayRandomValue(a);

 

 

从数字数组中获取最大值
var numbers = [1, 2, 3, 4, 5, 6];  
 
// Math对象内置方法max
var max = Math.max.apply(null, numbers);
 
// 数组排序方法sort
var max = numbers.sort(function(a, b){
    return b - a;
})[0];

 

 

JavaScript Snippet - Array【原】 2016-2-19

标签:

原文地址:http://www.cnblogs.com/happyfreelife/p/5379234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!