javascript - 用随机数填充 Javascript 中的二维数组
问题描述
我正在尝试用随机数填充 javascript 中的二维数组。尽管数组中的每一列都是随机的,但每一行都是相同的,这不是我想要的(见下图)。我希望行和列都是随机的。
http://eeldesigns.com/image.jpg
cols = 5;
rows = 10;
front = new Array(cols).fill(new Array(rows));
// Loop through Initial array to randomly place cells
for(var x = 0; x < cols; x++){
for(var y = 0; y < rows; y++){
front[x][y] = Math.floor(Math.random()*5);
}
}
console.table(front) ;
解决方案
使用地图的一种方法
let op = new Array(10)
.fill(0)
.map(e=>(new Array(5)
.fill(0)
.map(e=> Math.floor(Math.random() * 5))))
console.log(op)
推荐阅读
- javascript - 如何使用 jquery 传递数据提交表单
- python - 基于从同一行中的多个列中检查其他字符串来创建附加列
- javascript - 如何在网页上实现安全和隐藏未经授权的用户的代码?
- javascript - 如何搜索匹配的名称
- elasticsearch - 这是一个关于springDataElasticSearch如何分桶的问题
- linux - /proc/pid/fdinfo/fd_num 中的标志 02000002 是什么意思?
- python-3.x - 我在列表中有 DataFrame 的列和数据,我想将相关数据放入相关列
- php - 如何使用 PHP 和 PDO 和准备好的语句创建安全(避免 SQL 注入)分页?
- r - 当 clusterOptions 参数存在时,ObserveEvent 删除 addCircleMarkers 组——预期的行为?
- amazon-web-services - 为子域设置 Bitnami Nginx