Home > Web Front-end > JS Tutorial > body text

Implementing PHP's shuffle array function shuffle example in JavaScript_javascript skills

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

There is a very convenient function shuffle() in PHP for shuffling arrays. This function is used in many situations, but JavaScript arrays do not have this method. It doesn’t matter if you don’t have it. You can extend one. Do it yourself and you will have enough food and clothing. .

Please refresh the page to see the random sorting effect.

Copy code The code is as follows:


Output result:

Copy code The code is as follows:

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

Add a method to the array through prototype:

Copy code The code is as follows:


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
Popular Tutorials
More>
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!