迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

在 JavaScript 中从数组中选择一个随机元素

作者:迹忆客 最近更新:2024/03/16 浏览次数:

本文介绍如何在 JavaScript 中从数组中选择随机元素。它还强调了位运算符 NOT (~~) 和 OR (|) 的使用,这对于小尺寸数组很有用。


在 JavaScript 中从数组中选择一个随机值

我们可以使用以下方式从 JavaScript 中的数组中随机选择一个元素:

  • Math.random()array.lengthMath.floor() 一起使用。
  • 使用 LodashUnderscore.js.sample() 方法。
  • 使用位运算符 NOTOR

在 JavaScript 中使用 Math.random()array.lengthMath.floor() 从数组中选择一个随机元素

var arrStr = ['Mehvish', 'Tahir', 'John', 'Sania', 'Thomas'];
var randElement = arrStr[Math.floor(Math.random() * arrStr.length)];
console.log(randElement);

输出:

"John"

在上面的示例中,Math.random() 方法用于获取 01 之间的随机数,其中 1 是互斥的,0 是包含的。

然后,将它乘以数组的大小,得到 0 - array.length 之间的答案。

最后,我们使用 Math.floor() 来获取 0array.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 中使用 LodashUnderscore.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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便