在 JavaScript 中从数组中选择一个随机元素
本文介绍如何在 JavaScript 中从数组中选择随机元素。它还强调了位运算符 NOT
(~~
) 和 OR
(|
) 的使用,这对于小尺寸数组很有用。
在 JavaScript 中从数组中选择一个随机值
我们可以使用以下方式从 JavaScript 中的数组中随机选择一个元素:
Math.random()
、array.length
和Math.floor()
一起使用。- 使用
Lodash
和Underscore.js
的.sample()
方法。 - 使用位运算符
NOT
和OR
。
在 JavaScript 中使用 Math.random()
、array.length
和 Math.floor()
从数组中选择一个随机元素
var arrStr = ['Mehvish', 'Tahir', 'John', 'Sania', 'Thomas'];
var randElement = arrStr[Math.floor(Math.random() * arrStr.length)];
console.log(randElement);
输出:
"John"
在上面的示例中,Math.random()
方法用于获取 0
和 1
之间的随机数,其中 1
是互斥的,0
是包含的。
然后,将它乘以数组的大小,得到 0 - array.length
之间的答案。
最后,我们使用 Math.floor()
来获取 0
到 array.length-1
之间的索引。
var arrInt = [1, 3, 5, 7, 2, 9, 0];
var randElement = arrInt[Math.floor(Math.random() * arrInt.length)];
console.log(randElement);
输出:
9
在 JavaScript 中使用 Lodash
和 Underscore.js
的 .sample()
方法从数组中选择一个随机元素
let _ = require('lodash');
var arrStr = ['Mehvish', 'Tahir', 'John', 'Sania', 'Thomas'];
var randElement = _.sample(arrStr);
console.log(randElement);
输出:
"Sania"
在这里,我们使用 lodash 库的 .sample()
方法,该方法在另一个名为 Underscore.js
的库的顶部工作。
此方法采用单个参数、一个集合并从该集合中输出一个随机元素。
let _ = require('lodash');
var arrInt = [2, 5, 4, 7, 9, 0, 7];
var randElement = _.sample(arrInt);
console.log(randElement);
输出:
2
我们还可以使用 Underscore.js
库的 .sample()
方法。不同之处在于它需要两个参数:列表,第二个是数字。
它告诉你一次需要多少个随机元素。
var arrInt = [2, 5, 4, 7, 9, 0, 7];
var randElement = _.sample(arrInt);
console.log(randElement);
输出:
7
var arrInt = [2, 5, 4, 7, 9, 0, 7];
var randElement = _.sample(arrInt, 2);
console.log(randElement);
输出:
[2,9]
不要忘记在使用前导入 Underscore.js
。你可以在此处找到更多详细信息。
在 JavaScript 中使用位运算符 NOT
(~~
) 和 OR
(|
) 从数组中选择一个随机元素
var arrStr = ['Mehvish', 'Tahir', 'John', 'Sania', 'Thomas'];
var randElement = arrStr[~~(Math.random() * arrStr.length)];
console.log(randElement)
输出:
"Tahir"
上面的示例使用了 Math.floor()
方法的替代方法,即按位 NOT
(~~
) 运算符。
但是,它更快,但仅对小尺寸数组有用。当数组中有数百万个元素时,我们不能使用它。
让我们继续使用整数数组的 OR
运算符。对于小型数组,按位 OR
运算符也更快。
var arrInt = [2, 4, 6, 7, 3];
var randElement = arrInt[Math.random() * arrInt.length | 0];
console.log(randElement)
输出:
6
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。