JavaScript中实现PHP的打乱数组函数shuffle实例_javascript技巧

WBOY
Release: 2016-05-16 16:34:12
Original
1202 people have browsed it

PHP 里面有个非常方便的打乱数组的函数 shuffle() ,这个功能在许多情况下都会用到,但 javascript 的数组却没有这个方法,没有不要紧,可以扩展一个,自己动手,丰衣足食嘛。

请刷新页面查看随机排序效果。

复制代码代码如下:


输出结果:

复制代码代码如下:

A = 0,1,2,3,4,5,6,7,8,9

shuffle(A) = 1,5,0,9,2,3,6,8,4,7
A.shuffle() = 0,4,2,8,5,1,3,6,9,7

通过prototype 给数组添加一个方法:

复制代码代码如下:


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!