javascript - 如何使用地图洗牌数组
问题描述
我尝试过使用 map.shuffle 字符串单词数组。
该函数应该接受一个单词数组,例如:
["Apple","Coffe","Bananas"]
.
之后,它应该返回一个新的随机数组。一些随机的例如:
["Coffe",Apple,"Bananas]
.
但我很少明白如何做到这一点。
function shuffleArray(names) {
if (typeof names != "object")
throw "not an array"
var mapResult = names.map(function(item, index, array) {
return index + Math.floor(Math.random() * (index + 1));
});
return mapResult;
}
console.log(shuffleArray(["Apple","Coffe","Bananas"]))
解决方案
推荐阅读
- java - 过滤多个条件 - reactor Flux/Mono filterWhen()
- javascript - 我想在选择下拉选项后自动调用 api
- node.js - 反应开发服务器未启动
- regex - 删除除 New Line 之外的所有额外空格 - Power Automate Desktop 中的 Regex
- kubernetes - 如何收集 Kubernetes 元数据?
- wordpress - 某些小部件未显示在前端
- gitlab - GitLab API:按名称从管道中过滤作业
- java - Eclipse,Hibernate 5.0.7 锁定在调试模式
- r - 使用 nlme、ggeffects 和 sjplot 绘制 lme 模型对重复测量数据的总体水平预测
- python - UFT-8 Python LXML HTML