Home > Web Front-end > JS Tutorial > Use js to randomly return an element of the array_javascript skills

Use js to randomly return an element of the array_javascript skills

WBOY
Release: 2016-05-16 19:10:19
Original
1211 people have browsed it
Copy code The code is as follows:



This It's a wonderful method. Suitable for random rotation display of title text.

There are two different ways to do this:

1. Randomly pick a single one, 2. Let the entire array be randomly sorted

Note: The [ ] symbol defines an array in JavaScript, and { } defines an object

Randomly obtain one of the items in the array:

Copy code The code is as follows:


Randomly sort the entire array Array:

Copy code The code is as follows:


==========================================

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 it is not available. You can extend one. Do it yourself and you will have enough food and clothing. .

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